From 766097ff92b28f88947e9e583fe5f58457e322dc Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 14 Jun 2010 21:06:26 +0200 Subject: [PATCH 1/7] Define colors and fonts with the set function With this changes it is possible to set the colors with the :set function. --- config.def.h | 69 +++++++++++++++++++++----------- zathura.c | 111 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 114 insertions(+), 66 deletions(-) diff --git a/config.def.h b/config.def.h index d44425f..74593d2 100644 --- a/config.def.h +++ b/config.def.h @@ -20,28 +20,28 @@ static const char BM_PAGE_ENTRY[] = "page"; static const char BM_PAGE_OFFSET[] = "offset"; /* look */ -static const char font[] = "monospace normal 9"; -static const char default_bgcolor[] = "#000000"; -static const char default_fgcolor[] = "#DDDDDD"; -static const char inputbar_bgcolor[] = "#141414"; -static const char inputbar_fgcolor[] = "#9FBC00"; -static const char statusbar_bgcolor[] = "#000000"; -static const char statusbar_fgcolor[] = "#FFFFFF"; -static const char completion_fgcolor[] = "#DDDDDD"; -static const char completion_bgcolor[] = "#232323"; -static const char completion_g_fgcolor[] = "#DEDEDE"; -static const char completion_g_bgcolor[] = "#FF00FF"; -static const char completion_hl_fgcolor[] = "#232323"; -static const char completion_hl_bgcolor[] = "#9FBC00"; -static const char notification_e_bgcolor[] = "#FF1212"; -static const char notification_e_fgcolor[] = "#FFFFFF"; -static const char notification_w_bgcolor[] = "#FFF712"; -static const char notification_w_fgcolor[] = "#000000"; -static const char recolor_darkcolor[] = "#353535"; -static const char recolor_lightcolor[] = "#DBDBDB"; +char* font = "monospace normal 9"; +char* default_bgcolor = "#000000"; +char* default_fgcolor = "#DDDDDD"; +char* inputbar_bgcolor = "#141414"; +char* inputbar_fgcolor = "#9FBC00"; +char* statusbar_bgcolor = "#000000"; +char* statusbar_fgcolor = "#FFFFFF"; +char* completion_fgcolor = "#DDDDDD"; +char* completion_bgcolor = "#232323"; +char* completion_g_fgcolor = "#DEDEDE"; +char* completion_g_bgcolor = "#FF00FF"; +char* completion_hl_fgcolor = "#232323"; +char* completion_hl_bgcolor = "#9FBC00"; +char* notification_e_bgcolor = "#FF1212"; +char* notification_e_fgcolor = "#FFFFFF"; +char* notification_w_bgcolor = "#FFF712"; +char* notification_w_fgcolor = "#000000"; +char* recolor_darkcolor = "#353535"; +char* recolor_lightcolor = "#DBDBDB"; -static const char search_highlight[] = "#9FBC00"; -static const char select_text[] = "#000000"; +char* search_highlight = "#9FBC00"; +char* select_text = "#000000"; /* statusbar */ static const char DEFAULT_TEXT[] = "[No Name]"; @@ -183,7 +183,28 @@ SpecialCommand special_commands[] = { /* settings */ Setting settings[] = { - /* name, variable, type, render, description */ - {"recolor", &(Zathura.Global.recolor), 'b', TRUE, "Invert the image"}, - {"offset", &(Zathura.PDF.page_offset), 'i', FALSE, "Optional page offset"}, + /* name, variable, type, render, re-init, description */ + {"recolor", &(Zathura.Global.recolor), 'b', TRUE, FALSE, "Invert the image" }, + {"offset", &(Zathura.PDF.page_offset), 'i', FALSE, FALSE, "Optional page offset" }, + {"font", &(font), 's', FALSE, TRUE, "The used font" }, + {"default_bgcolor", &(default_bgcolor), 's', FALSE, TRUE, "Default background color"}, + {"default_fgcolor", &(default_fgcolor), 's', FALSE, TRUE, "Default foreground color"}, + {"inputbar_bgcolor", &(inputbar_bgcolor), 's', FALSE, TRUE, "Inputbar background color"}, + {"inputbar_fgcolor", &(inputbar_fgcolor), 's', FALSE, TRUE, "Inputbar foreground color"}, + {"statusbar_bgcolor", &(statusbar_bgcolor), 's', FALSE, TRUE, "Statusbar background color"}, + {"statusbar_fgcolor", &(statusbar_fgcolor), 's', FALSE, TRUE, "Statusbar foreground color"}, + {"completion_bgcolor", &(completion_bgcolor), 's', FALSE, TRUE, "Completion background color"}, + {"completion_fgcolor", &(completion_fgcolor), 's', FALSE, TRUE, "Completion foreground color"}, + {"completion_g_bgcolor", &(completion_g_bgcolor), 's', FALSE, TRUE, "Completion (group) background color"}, + {"completion_g_fgcolor", &(completion_g_fgcolor), 's', FALSE, TRUE, "Completion (group) foreground color"}, + {"completion_hl_bgcolor", &(completion_hl_bgcolor), 's', FALSE, TRUE, "Completion (highlight) background color"}, + {"completion_hl_fgcolor", &(completion_hl_fgcolor), 's', FALSE, TRUE, "Completion (highlight) foreground color"}, + {"notification_e_bgcolor", &(notification_e_bgcolor), 's', FALSE, TRUE, "Notification (error) background color"}, + {"notification_e_fgcolor", &(notification_e_fgcolor), 's', FALSE, TRUE, "Notification (error) foreground color"}, + {"notification_w_bgcolor", &(notification_w_bgcolor), 's', FALSE, TRUE, "Notification (warning) background color"}, + {"notification_w_fgcolor", &(notification_w_fgcolor), 's', FALSE, TRUE, "Notification (warning) foreground color"}, + {"recolor_darkcolor", &(recolor_darkcolor), 's', FALSE, TRUE, "Recoloring (dark color)"}, + {"recolor_lightcolor", &(recolor_lightcolor), 's', FALSE, TRUE, "Recoloring (light color)"}, + {"search_highlight", &(search_highlight), 's', FALSE, TRUE, "Highlighted results"}, + {"select_text", &(select_text), 's', FALSE, TRUE, "Rectangle of the selected text"}, }; diff --git a/zathura.c b/zathura.c index 3fc8c96..7472e87 100644 --- a/zathura.c +++ b/zathura.c @@ -130,6 +130,7 @@ typedef struct void* variable; char type; gboolean render; + gboolean reinit; char* description; } Setting; @@ -284,6 +285,7 @@ struct } Zathura; /* function declarations */ +void init_colors(); void init_directories(); void init_zathura(); void add_marker(int); @@ -386,6 +388,54 @@ gboolean cb_watch_file(GFileMonitor*, GFile*, GFile*, GFileMonitorEvent, gpointe #include "config.h" /* function implementation */ +void +init_colors() +{ + /* parse */ + gdk_color_parse(default_fgcolor, &(Zathura.Style.default_fg)); + gdk_color_parse(default_bgcolor, &(Zathura.Style.default_bg)); + gdk_color_parse(inputbar_fgcolor, &(Zathura.Style.inputbar_fg)); + gdk_color_parse(inputbar_bgcolor, &(Zathura.Style.inputbar_bg)); + gdk_color_parse(statusbar_fgcolor, &(Zathura.Style.statusbar_fg)); + gdk_color_parse(statusbar_bgcolor, &(Zathura.Style.statusbar_bg)); + gdk_color_parse(completion_fgcolor, &(Zathura.Style.completion_fg)); + gdk_color_parse(completion_bgcolor, &(Zathura.Style.completion_bg)); + gdk_color_parse(completion_g_fgcolor, &(Zathura.Style.completion_g_fg)); + gdk_color_parse(completion_g_fgcolor, &(Zathura.Style.completion_g_fg)); + gdk_color_parse(completion_hl_fgcolor, &(Zathura.Style.completion_hl_fg)); + gdk_color_parse(completion_hl_bgcolor, &(Zathura.Style.completion_hl_bg)); + gdk_color_parse(notification_e_fgcolor, &(Zathura.Style.notification_e_fg)); + gdk_color_parse(notification_e_bgcolor, &(Zathura.Style.notification_e_bg)); + gdk_color_parse(notification_w_fgcolor, &(Zathura.Style.notification_w_fg)); + gdk_color_parse(notification_w_bgcolor, &(Zathura.Style.notification_w_bg)); + gdk_color_parse(recolor_darkcolor, &(Zathura.Style.recolor_darkcolor)); + gdk_color_parse(recolor_lightcolor, &(Zathura.Style.recolor_lightcolor)); + gdk_color_parse(search_highlight, &(Zathura.Style.search_highlight)); + gdk_color_parse(select_text, &(Zathura.Style.select_text)); + Zathura.Style.font = pango_font_description_from_string(font); + + /* drawing area */ + gtk_widget_modify_bg(GTK_WIDGET(Zathura.UI.drawing_area), GTK_STATE_NORMAL, &(Zathura.Style.default_bg)); + + /* statusbar */ + gtk_widget_modify_bg(GTK_WIDGET(Zathura.UI.statusbar), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_bg)); + + gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_text), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); + gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_state), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); + gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_buffer), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); + + gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_text), Zathura.Style.font); + gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_state), Zathura.Style.font); + gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_buffer), Zathura.Style.font); + + /* inputbar */ + gtk_widget_modify_base(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_bg)); + gtk_widget_modify_text(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_fg)); + gtk_widget_modify_font(GTK_WIDGET(Zathura.UI.inputbar), Zathura.Style.font); + + +} + void init_directories() { @@ -427,29 +477,6 @@ init_zathura() g_static_mutex_init(&(Zathura.Lock.select_lock)); g_static_mutex_init(&(Zathura.Lock.select_lock)); - /* look */ - gdk_color_parse(default_fgcolor, &(Zathura.Style.default_fg)); - gdk_color_parse(default_bgcolor, &(Zathura.Style.default_bg)); - gdk_color_parse(inputbar_fgcolor, &(Zathura.Style.inputbar_fg)); - gdk_color_parse(inputbar_bgcolor, &(Zathura.Style.inputbar_bg)); - gdk_color_parse(statusbar_fgcolor, &(Zathura.Style.statusbar_fg)); - gdk_color_parse(statusbar_bgcolor, &(Zathura.Style.statusbar_bg)); - gdk_color_parse(completion_fgcolor, &(Zathura.Style.completion_fg)); - gdk_color_parse(completion_bgcolor, &(Zathura.Style.completion_bg)); - gdk_color_parse(completion_g_fgcolor, &(Zathura.Style.completion_g_fg)); - gdk_color_parse(completion_g_fgcolor, &(Zathura.Style.completion_g_fg)); - gdk_color_parse(completion_hl_fgcolor, &(Zathura.Style.completion_hl_fg)); - gdk_color_parse(completion_hl_bgcolor, &(Zathura.Style.completion_hl_bg)); - gdk_color_parse(notification_e_fgcolor, &(Zathura.Style.notification_e_fg)); - gdk_color_parse(notification_e_bgcolor, &(Zathura.Style.notification_e_bg)); - gdk_color_parse(notification_w_fgcolor, &(Zathura.Style.notification_w_fg)); - gdk_color_parse(notification_w_bgcolor, &(Zathura.Style.notification_w_bg)); - gdk_color_parse(recolor_darkcolor, &(Zathura.Style.recolor_darkcolor)); - gdk_color_parse(recolor_lightcolor, &(Zathura.Style.recolor_lightcolor)); - gdk_color_parse(search_highlight, &(Zathura.Style.search_highlight)); - gdk_color_parse(select_text, &(Zathura.Style.select_text)); - Zathura.Style.font = pango_font_description_from_string(font); - /* other */ Zathura.Global.mode = NORMAL; Zathura.Global.viewing_mode = NORMAL; @@ -523,25 +550,14 @@ init_zathura() #endif /* drawing area */ - gtk_widget_modify_bg(GTK_WIDGET(Zathura.UI.drawing_area), GTK_STATE_NORMAL, &(Zathura.Style.default_bg)); gtk_widget_show(Zathura.UI.drawing_area); g_signal_connect(G_OBJECT(Zathura.UI.drawing_area), "expose-event", G_CALLBACK(cb_draw), NULL); /* statusbar */ - gtk_widget_modify_bg(GTK_WIDGET(Zathura.UI.statusbar), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_bg)); - Zathura.Global.status_text = GTK_LABEL(gtk_label_new(NULL)); Zathura.Global.status_state = GTK_LABEL(gtk_label_new(NULL)); Zathura.Global.status_buffer = GTK_LABEL(gtk_label_new(NULL)); - gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_text), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); - gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_state), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); - gtk_widget_modify_fg(GTK_WIDGET(Zathura.Global.status_buffer), GTK_STATE_NORMAL, &(Zathura.Style.statusbar_fg)); - - gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_text), Zathura.Style.font); - gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_state), Zathura.Style.font); - gtk_widget_modify_font(GTK_WIDGET(Zathura.Global.status_buffer), Zathura.Style.font); - gtk_misc_set_alignment(GTK_MISC(Zathura.Global.status_text), 0.0, 0.0); gtk_misc_set_alignment(GTK_MISC(Zathura.Global.status_state), 1.0, 0.0); gtk_misc_set_alignment(GTK_MISC(Zathura.Global.status_buffer), 1.0, 0.0); @@ -565,10 +581,6 @@ init_zathura() gtk_entry_set_has_frame( Zathura.UI.inputbar, FALSE); gtk_editable_set_editable( GTK_EDITABLE(Zathura.UI.inputbar), TRUE); - gtk_widget_modify_base(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_bg)); - gtk_widget_modify_text(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_fg)); - gtk_widget_modify_font(GTK_WIDGET(Zathura.UI.inputbar), Zathura.Style.font); - Zathura.Handler.inputbar_key_press_event = g_signal_connect(G_OBJECT(Zathura.UI.inputbar), "key-press-event", G_CALLBACK(cb_inputbar_kb_pressed), NULL); Zathura.Handler.inputbar_activate = @@ -2682,7 +2694,7 @@ cmd_rotate(int argc, char** argv) gboolean cmd_set(int argc, char** argv) { - if(argc <= 0 || argc >= 3) + if(argc <= 0) return FALSE; int i; @@ -2715,12 +2727,22 @@ cmd_set(int argc, char** argv) } else if(settings[i].type == 's') { - if(argc != 2) + if(argc < 2) return FALSE; + /* assembly the arguments back to one string */ + int i; + GString *s = g_string_new(""); + for(i = 1; i < argc; i++) + { + if(i != 0) + s = g_string_append_c(s, ' '); + + s = g_string_append(s, argv[i]); + } + char **x = (char**) settings[i].variable; - if(argv[1]) - *x = argv[1]; + *x = s->str; } else if(settings[i].type == 'c') { @@ -2732,6 +2754,10 @@ cmd_set(int argc, char** argv) *x = argv[1][0]; } + /* re-init */ + if(settings[i].reinit) + init_colors(); + /* render */ if(settings[i].render) { @@ -3675,6 +3701,7 @@ int main(int argc, char* argv[]) gtk_init(&argc, &argv); init_zathura(); + init_colors(); init_directories(); if(argc >= 2) From a27d31d9b50f0e024e6b6c5e3f13edb4b7eae500 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 14 Jun 2010 21:52:19 +0200 Subject: [PATCH 2/7] Introduced zathurarc --- config.def.h | 1 + zathura.c | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/config.def.h b/config.def.h index 74593d2..f2913e0 100644 --- a/config.def.h +++ b/config.def.h @@ -14,6 +14,7 @@ static const char FORMAT_DESCRIPTION[] = "%s"; /* directories and files */ static const char ZATHURA_DIR[] = ".zathura"; static const char BOOKMARK_FILE[] = "bookmarks"; +static const char ZATHURA_RC[] = "zathurarc"; /* bookmarks */ static const char BM_PAGE_ENTRY[] = "page"; diff --git a/zathura.c b/zathura.c index 7472e87..286a3ad 100644 --- a/zathura.c +++ b/zathura.c @@ -300,6 +300,7 @@ void notify(int, char*); gboolean open_file(char*, char*); void open_uri(char*); void update_status(); +void read_configuration(); void recalcRectangle(int, PopplerRectangle*); void setCompletionRowColor(GtkBox*, int, int); void set_page(int); @@ -1124,6 +1125,38 @@ update_status() g_free(zoom_level); } +void +read_configuration() +{ + char* zathurarc = g_strdup_printf("%s/%s/%s", g_get_home_dir(), ZATHURA_DIR, ZATHURA_RC); + + if(!zathurarc) + return; + + if(g_file_test(zathurarc, G_FILE_TEST_IS_REGULAR)) + { + char* content = NULL; + + if(g_file_get_contents(zathurarc, &content, NULL, NULL)) + { + gchar **lines = g_strsplit(content, "\n", -1); + int n = g_strv_length(lines) - 1; + + int i; + for(i = 0; i < n; i++) + { + gchar **tokens = g_strsplit(lines[i], " ", -1); + int length = g_strv_length(tokens); + + if(!strcmp(tokens[0], "set")) + cmd_set(length - 1, tokens + 1); + } + } + } + + g_free(zathurarc); +} + void recalcRectangle(int page_id, PopplerRectangle* rectangle) { @@ -2731,14 +2764,14 @@ cmd_set(int argc, char** argv) return FALSE; /* assembly the arguments back to one string */ - int i; + int j; GString *s = g_string_new(""); - for(i = 1; i < argc; i++) + for(j = 1; j < argc; j++) { - if(i != 0) + if(j != 1) s = g_string_append_c(s, ' '); - s = g_string_append(s, argv[i]); + s = g_string_append(s, argv[j]); } char **x = (char**) settings[i].variable; @@ -3701,6 +3734,7 @@ int main(int argc, char* argv[]) gtk_init(&argc, &argv); init_zathura(); + read_configuration(); init_colors(); init_directories(); From 4b62f58b31192830a2a972d5a5ea7912fcdb0348 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Tue, 15 Jun 2010 12:50:43 +0200 Subject: [PATCH 3/7] More options available in zathurarc This commit makes much more settings available in the zathurarc file. --- config.def.h | 86 ++++++++++++++++++++++++++++++---------------------- zathura.c | 83 +++++++++++++++++++++++++++++--------------------- 2 files changed, 98 insertions(+), 71 deletions(-) diff --git a/config.def.h b/config.def.h index f2913e0..be2be7d 100644 --- a/config.def.h +++ b/config.def.h @@ -1,11 +1,11 @@ /* settings */ -static const int DEFAULT_WIDTH = 800; -static const int DEFAULT_HEIGHT = 600; -static const float ZOOM_STEP = 10; -static const float ZOOM_MIN = 10; -static const float ZOOM_MAX = 400; -static const float SCROLL_STEP = 40; -static const float TRANSPARENCY = 0.4; +int default_width = 800; +int default_height = 600; +float zoom_step = 10; +float zoom_min = 10; +float zoom_max = 400; +float scroll_step = 40; +float transparency = 0.4; /* completion */ static const char FORMAT_COMMAND[] = "%s"; @@ -45,19 +45,18 @@ char* search_highlight = "#9FBC00"; char* select_text = "#000000"; /* statusbar */ -static const char DEFAULT_TEXT[] = "[No Name]"; +char* default_text = "[No Name]"; /* printing */ -#define LIST_PRINTER_COMMAND "lpstat -v | sed -n '/^.*device for \\(.*\\): .*$/s//\\1/p'" -#define PRINT_COMMAND "lp -d '%s' -P %s '%s'" /* printer / pages / file */ +char* list_printer_command = "lpstat -v | sed -n '/^.*device for \\(.*\\): .*$/s//\\1/p'"; +char* print_command = "lp -d '%s' -P %s '%s'"; /* printer / pages / file */ /* open uri */ -#define URI_COMMAND "firefox '%s'" /* uri */ +char* uri_command = "firefox '%s'"; /* uri */ /* additional settings */ -#define SHOW_SCROLLBARS 0 +gboolean show_scrollbars = FALSE; #define ADJUST_OPEN ADJUST_BESTFIT -#define RECOLOR_OPEN 0 #define SELECTION_STYLE POPPLER_SELECTION_GLYPH #define GOTO_MODE GOTO_LABELS /* GOTO_DEFAULT, GOTO_LABELS, GOTO_OFFSET */ @@ -184,28 +183,41 @@ SpecialCommand special_commands[] = { /* settings */ Setting settings[] = { - /* name, variable, type, render, re-init, description */ - {"recolor", &(Zathura.Global.recolor), 'b', TRUE, FALSE, "Invert the image" }, - {"offset", &(Zathura.PDF.page_offset), 'i', FALSE, FALSE, "Optional page offset" }, - {"font", &(font), 's', FALSE, TRUE, "The used font" }, - {"default_bgcolor", &(default_bgcolor), 's', FALSE, TRUE, "Default background color"}, - {"default_fgcolor", &(default_fgcolor), 's', FALSE, TRUE, "Default foreground color"}, - {"inputbar_bgcolor", &(inputbar_bgcolor), 's', FALSE, TRUE, "Inputbar background color"}, - {"inputbar_fgcolor", &(inputbar_fgcolor), 's', FALSE, TRUE, "Inputbar foreground color"}, - {"statusbar_bgcolor", &(statusbar_bgcolor), 's', FALSE, TRUE, "Statusbar background color"}, - {"statusbar_fgcolor", &(statusbar_fgcolor), 's', FALSE, TRUE, "Statusbar foreground color"}, - {"completion_bgcolor", &(completion_bgcolor), 's', FALSE, TRUE, "Completion background color"}, - {"completion_fgcolor", &(completion_fgcolor), 's', FALSE, TRUE, "Completion foreground color"}, - {"completion_g_bgcolor", &(completion_g_bgcolor), 's', FALSE, TRUE, "Completion (group) background color"}, - {"completion_g_fgcolor", &(completion_g_fgcolor), 's', FALSE, TRUE, "Completion (group) foreground color"}, - {"completion_hl_bgcolor", &(completion_hl_bgcolor), 's', FALSE, TRUE, "Completion (highlight) background color"}, - {"completion_hl_fgcolor", &(completion_hl_fgcolor), 's', FALSE, TRUE, "Completion (highlight) foreground color"}, - {"notification_e_bgcolor", &(notification_e_bgcolor), 's', FALSE, TRUE, "Notification (error) background color"}, - {"notification_e_fgcolor", &(notification_e_fgcolor), 's', FALSE, TRUE, "Notification (error) foreground color"}, - {"notification_w_bgcolor", &(notification_w_bgcolor), 's', FALSE, TRUE, "Notification (warning) background color"}, - {"notification_w_fgcolor", &(notification_w_fgcolor), 's', FALSE, TRUE, "Notification (warning) foreground color"}, - {"recolor_darkcolor", &(recolor_darkcolor), 's', FALSE, TRUE, "Recoloring (dark color)"}, - {"recolor_lightcolor", &(recolor_lightcolor), 's', FALSE, TRUE, "Recoloring (light color)"}, - {"search_highlight", &(search_highlight), 's', FALSE, TRUE, "Highlighted results"}, - {"select_text", &(select_text), 's', FALSE, TRUE, "Rectangle of the selected text"}, + /* name, variable, type, render, re-init, description */ + {"browser", &(uri_command), 's', FALSE, FALSE, "Command to open URIs"}, + {"completion_bgcolor", &(completion_bgcolor), 's', FALSE, TRUE, "Completion background color"}, + {"completion_fgcolor", &(completion_fgcolor), 's', FALSE, TRUE, "Completion foreground color"}, + {"completion_g_bgcolor", &(completion_g_bgcolor), 's', FALSE, TRUE, "Completion (group) background color"}, + {"completion_g_fgcolor", &(completion_g_fgcolor), 's', FALSE, TRUE, "Completion (group) foreground color"}, + {"completion_hl_bgcolor", &(completion_hl_bgcolor), 's', FALSE, TRUE, "Completion (highlight) background color"}, + {"completion_hl_fgcolor", &(completion_hl_fgcolor), 's', FALSE, TRUE, "Completion (highlight) foreground color"}, + {"default_bgcolor", &(default_bgcolor), 's', FALSE, TRUE, "Default background color"}, + {"default_fgcolor", &(default_fgcolor), 's', FALSE, TRUE, "Default foreground color"}, + {"default_text", &(default_text), 's', FALSE, FALSE, "Default text"}, + {"font", &(font), 's', FALSE, TRUE, "The used font" }, + {"height", &(default_height), 'i', FALSE, FALSE, "Default window height"}, + {"inputbar_bgcolor", &(inputbar_bgcolor), 's', FALSE, TRUE, "Inputbar background color"}, + {"inputbar_fgcolor", &(inputbar_fgcolor), 's', FALSE, TRUE, "Inputbar foreground color"}, + {"labels", &(Zathura.Global.enable_labelmode), 'b', FALSE, TRUE, "Allow label mode"}, + {"list_printer_command", &(list_printer_command), 's', FALSE, FALSE, "Command to list printers"}, + {"notification_e_bgcolor", &(notification_e_bgcolor), 's', FALSE, TRUE, "Notification (error) background color"}, + {"notification_e_fgcolor", &(notification_e_fgcolor), 's', FALSE, TRUE, "Notification (error) foreground color"}, + {"notification_w_bgcolor", &(notification_w_bgcolor), 's', FALSE, TRUE, "Notification (warning) background color"}, + {"notification_w_fgcolor", &(notification_w_fgcolor), 's', FALSE, TRUE, "Notification (warning) foreground color"}, + {"offset", &(Zathura.PDF.page_offset), 'i', FALSE, FALSE, "Optional page offset" }, + {"print_command", &(print_command), 's', FALSE, FALSE, "Command to print"}, + {"recolor", &(Zathura.Global.recolor), 'b', TRUE, FALSE, "Invert the image" }, + {"recolor_darkcolor", &(recolor_darkcolor), 's', FALSE, TRUE, "Recoloring (dark color)"}, + {"recolor_lightcolor", &(recolor_lightcolor), 's', FALSE, TRUE, "Recoloring (light color)"}, + {"scroll_step", &(scroll_step), 'f', FALSE, FALSE, "Scroll step"}, + {"scrollbars", &(show_scrollbars), 'b', FALSE, TRUE, "Show scrollbars"}, + {"search_highlight", &(search_highlight), 's', FALSE, TRUE, "Highlighted results"}, + {"select_text", &(select_text), 's', FALSE, TRUE, "Rectangle of the selected text"}, + {"statusbar_bgcolor", &(statusbar_bgcolor), 's', FALSE, TRUE, "Statusbar background color"}, + {"statusbar_fgcolor", &(statusbar_fgcolor), 's', FALSE, TRUE, "Statusbar foreground color"}, + {"transparency", &(transparency), 'f', FALSE, FALSE, "Transparency of rectangles"}, + {"width", &(default_width), 'i', FALSE, FALSE, "Default window width"}, + {"zoom_max", &(zoom_max), 'f', FALSE, FALSE, "Zoom maximum"}, + {"zoom_min", &(zoom_min), 'f', FALSE, FALSE, "Zoom minimum"}, + {"zoom_step", &(zoom_step), 'f', FALSE, FALSE, "Zoom step"}, }; diff --git a/zathura.c b/zathura.c index 286a3ad..a9391e8 100644 --- a/zathura.c +++ b/zathura.c @@ -285,8 +285,9 @@ struct } Zathura; /* function declarations */ -void init_colors(); +void init_look(); void init_directories(); +void init_settings(); void init_zathura(); void add_marker(int); void build_index(GtkTreeModel*, GtkTreeIter*, PopplerIndexIter*); @@ -390,7 +391,7 @@ gboolean cb_watch_file(GFileMonitor*, GFile*, GFile*, GFileMonitorEvent, gpointe /* function implementation */ void -init_colors() +init_look() { /* parse */ gdk_color_parse(default_fgcolor, &(Zathura.Style.default_fg)); @@ -434,7 +435,11 @@ init_colors() gtk_widget_modify_text(GTK_WIDGET(Zathura.UI.inputbar), GTK_STATE_NORMAL, &(Zathura.Style.inputbar_fg)); gtk_widget_modify_font(GTK_WIDGET(Zathura.UI.inputbar), Zathura.Style.font); - + /* scrollbars */ + if(show_scrollbars) + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + else + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_NEVER, GTK_POLICY_NEVER); } void @@ -468,6 +473,14 @@ init_directories() g_free(bookmarks); } +void +init_settings() +{ + Zathura.State.filename = (char*) default_text; + + gtk_window_set_default_size(Zathura.UI.window, default_width, default_height); +} + void init_zathura() { @@ -481,12 +494,11 @@ init_zathura() /* other */ Zathura.Global.mode = NORMAL; Zathura.Global.viewing_mode = NORMAL; - Zathura.Global.recolor = RECOLOR_OPEN; + Zathura.Global.recolor = 0; Zathura.Global.adjust_mode = ADJUST_OPEN; Zathura.Global.goto_mode = GOTO_MODE; Zathura.Global.show_index = FALSE; - Zathura.State.filename = (char*) DEFAULT_TEXT; Zathura.State.pages = g_strdup_printf(""); Zathura.State.scroll_percentage = 0; @@ -517,7 +529,6 @@ init_zathura() gtk_window_set_title(Zathura.UI.window, "zathura"); GdkGeometry hints = { 1, 1 }; gtk_window_set_geometry_hints(Zathura.UI.window, NULL, &hints, GDK_HINT_MIN_SIZE); - gtk_window_set_default_size(Zathura.UI.window, DEFAULT_WIDTH, DEFAULT_HEIGHT); g_signal_connect(G_OBJECT(Zathura.UI.window), "destroy", G_CALLBACK(cb_destroy), NULL); /* box */ @@ -544,12 +555,6 @@ init_zathura() gtk_container_add(GTK_CONTAINER(Zathura.UI.view), GTK_WIDGET(Zathura.UI.viewport)); gtk_viewport_set_shadow_type(Zathura.UI.viewport, GTK_SHADOW_NONE); - #if SHOW_SCROLLBARS - gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); - #else - gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.view), GTK_POLICY_NEVER, GTK_POLICY_NEVER); - #endif - /* drawing area */ gtk_widget_show(Zathura.UI.drawing_area); g_signal_connect(G_OBJECT(Zathura.UI.drawing_area), "expose-event", G_CALLBACK(cb_draw), NULL); @@ -877,7 +882,7 @@ highlight_result(int page_id, PopplerRectangle* rectangle) PopplerRectangle* trect = poppler_rectangle_copy(rectangle); cairo_t *cairo = cairo_create(Zathura.PDF.surface); cairo_set_source_rgba(cairo, Zathura.Style.search_highlight.red, Zathura.Style.search_highlight.green, - Zathura.Style.search_highlight.blue, TRANSPARENCY); + Zathura.Style.search_highlight.blue, transparency); recalcRectangle(page_id, trect); cairo_rectangle(cairo, trect->x1, trect->y1, (trect->x2 - trect->x1), (trect->y2 - trect->y1)); @@ -1095,7 +1100,7 @@ open_file(char* path, char* password) void open_uri(char* uri) { - char* uri_cmd = g_strdup_printf(URI_COMMAND, uri); + char* uri_cmd = g_strdup_printf(uri_command, uri); system(uri_cmd); g_free(uri_cmd); } @@ -1670,13 +1675,13 @@ sc_scroll(Argument* argument) else if(argument->n == HALF_DOWN) gtk_adjustment_set_value(adjustment, (value + (view_size / 2)) > max ? max : (value + (view_size / 2))); else if((argument->n == LEFT) || (argument->n == UP)) - gtk_adjustment_set_value(adjustment, (value - SCROLL_STEP) < 0 ? 0 : (value - SCROLL_STEP)); + gtk_adjustment_set_value(adjustment, (value - scroll_step) < 0 ? 0 : (value - scroll_step)); else if(argument->n == TOP) gtk_adjustment_set_value(adjustment, 0); else if(argument->n == BOTTOM) gtk_adjustment_set_value(adjustment, max); else - gtk_adjustment_set_value(adjustment, (value + SCROLL_STEP) > max ? max : (value + SCROLL_STEP)); + gtk_adjustment_set_value(adjustment, (value + scroll_step) > max ? max : (value + scroll_step)); update_status(); } @@ -2398,7 +2403,7 @@ cmd_close(int argc, char** argv) gtk_window_set_title(Zathura.UI.window, "zathura"); Zathura.State.pages = g_strdup_printf(""); - Zathura.State.filename = (char*) DEFAULT_TEXT; + Zathura.State.filename = (char*) default_text; g_static_mutex_lock(&(Zathura.Lock.pdf_obj_lock)); Zathura.PDF.document = NULL; @@ -2709,11 +2714,11 @@ cmd_print(int argc, char** argv) char* printer = argv[0]; char* sites = (argc == 2) ? g_strdup(argv[1]) : g_strdup_printf("1-%i", Zathura.PDF.number_of_pages); - char* print_command = g_strdup_printf(PRINT_COMMAND, printer, sites, Zathura.PDF.file); - system(print_command); + char* command = g_strdup_printf(print_command, printer, sites, Zathura.PDF.file); + system(command); g_free(sites); - g_free(print_command); + g_free(command); return TRUE; } @@ -2758,6 +2763,15 @@ cmd_set(int argc, char** argv) if(argv[1]) *x = atoi(argv[1]); } + else if(settings[i].type == 'f') + { + if(argc != 2) + return FALSE; + + float *x = (float*) (settings[i].variable); + if(argv[1]) + *x = atof(argv[1]); + } else if(settings[i].type == 's') { if(argc < 2) @@ -2789,7 +2803,7 @@ cmd_set(int argc, char** argv) /* re-init */ if(settings[i].reinit) - init_colors(); + init_look(); /* render */ if(settings[i].render) @@ -3009,7 +3023,7 @@ cc_print(char* input) int count = 0; FILE *fp; - fp = popen(LIST_PRINTER_COMMAND, "r"); + fp = popen(list_printer_command, "r"); if(!fp) { @@ -3160,17 +3174,17 @@ bcmd_zoom(char* buffer, Argument* argument) if(argument->n == ZOOM_IN) { - if((Zathura.PDF.scale + ZOOM_STEP) <= ZOOM_MAX) - Zathura.PDF.scale += ZOOM_STEP; + if((Zathura.PDF.scale + zoom_step) <= zoom_max) + Zathura.PDF.scale += zoom_step; else - Zathura.PDF.scale = ZOOM_MAX; + Zathura.PDF.scale = zoom_max; } else if(argument->n == ZOOM_OUT) { - if((Zathura.PDF.scale - ZOOM_STEP) >= ZOOM_MIN) - Zathura.PDF.scale -= ZOOM_STEP; + if((Zathura.PDF.scale - zoom_step) >= zoom_min) + Zathura.PDF.scale -= zoom_step; else - Zathura.PDF.scale = ZOOM_MIN; + Zathura.PDF.scale = zoom_min; } else if(argument->n == ZOOM_SPECIFIC) { @@ -3179,10 +3193,10 @@ bcmd_zoom(char* buffer, Argument* argument) return; int value = atoi(g_strndup(buffer, b_length - 1)); - if(value <= ZOOM_MIN) - Zathura.PDF.scale = ZOOM_MIN; - else if(value >= ZOOM_MAX) - Zathura.PDF.scale = ZOOM_MAX; + if(value <= zoom_min) + Zathura.PDF.scale = zoom_min; + else if(value >= zoom_max) + Zathura.PDF.scale = zoom_max; else Zathura.PDF.scale = value; } @@ -3610,7 +3624,7 @@ cb_view_button_release(GtkWidget* widget, GdkEventButton* event, gpointer data) /* draw selection rectangle */ cairo = cairo_create(Zathura.PDF.surface); cairo_set_source_rgba(cairo, Zathura.Style.select_text.red, Zathura.Style.select_text.green, - Zathura.Style.select_text.blue, TRANSPARENCY); + Zathura.Style.select_text.blue, transparency); cairo_rectangle(cairo, rectangle.x1 - offset_x, rectangle.y1 - offset_y, (rectangle.x2 - rectangle.x1), (rectangle.y2 - rectangle.y1)); cairo_fill(cairo); @@ -3735,7 +3749,8 @@ int main(int argc, char* argv[]) init_zathura(); read_configuration(); - init_colors(); + init_settings(); + init_look(); init_directories(); if(argc >= 2) From dff4535c353b4be05cb934c92377a8fd8c9ea01d Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 16 Jun 2010 21:50:28 +0200 Subject: [PATCH 4/7] Added blank map function --- config.def.h | 5 +++-- zathura.c | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/config.def.h b/config.def.h index be2be7d..548cec2 100644 --- a/config.def.h +++ b/config.def.h @@ -146,18 +146,19 @@ MouseScrollEvent mouse_scroll_events[] = { /* commands */ Command commands[] = { /* command, abbreviation, function, completion, description */ - {"bmark", "b", cmd_bookmark, 0, "Bookmark current page" }, {"blist", 0, cmd_open_bookmark, cc_bookmark, "List and open bookmark" }, + {"bmark", "b", cmd_bookmark, 0, "Bookmark current page" }, {"close", "c", cmd_close, 0, "Close current file" }, {"coffset", 0, cmd_correct_offset, 0, "Correct page offset" }, {"delbmark", 0, cmd_delete_bookmark, cc_bookmark, "Bookmark current page" }, {"export", "e", cmd_export, cc_export, "Export images or attached files" }, {"info", "i", cmd_info, 0, "Show information about the document" }, + {"map", "m", cmd_map, 0, "Map keybinding to a function" }, {"open", "o", cmd_open, cc_open, "Open a file" }, {"print", "p", cmd_print, cc_print, "Print the document" }, + {"quit", "q", cmd_quit, 0, "Quit zathura" }, {"rotate", "r", cmd_rotate, 0, "Rotate the page" }, {"set", "s", cmd_set, cc_set, "Set an option" }, - {"quit", "q", cmd_quit, 0, "Quit zathura" }, {"write", "w", cmd_save, 0, "Save the document" }, }; diff --git a/zathura.c b/zathura.c index a9391e8..8ff9ef5 100644 --- a/zathura.c +++ b/zathura.c @@ -346,6 +346,7 @@ gboolean cmd_correct_offset(int, char**); gboolean cmd_delete_bookmark(int, char**); gboolean cmd_export(int, char**); gboolean cmd_info(int, char**); +gboolean cmd_map(int, char**); gboolean cmd_open(int, char**); gboolean cmd_print(int, char**); gboolean cmd_rotate(int, char**); @@ -2680,6 +2681,15 @@ cmd_info(int argc, char** argv) return FALSE; } +gboolean +cmd_map(int argc, char** argv) +{ + if(argc < 2) + return FALSE; + + return TRUE; +} + gboolean cmd_open(int argc, char** argv) { From ac0461d3575c2eb2095db4e6bbccb7fe5f745d26 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 16 Jun 2010 22:42:45 +0200 Subject: [PATCH 5/7] Introduce a shortcut list This commit introduces a shortcut list that is used now to check given keybindgins: On the contrary to the static shortcut array it can be easily modified and extended. In addition some malloc-checks have been introduced and a named shortcut list that will be used to evaluate the cmd_map parameters. --- config.def.h | 23 ++++++++ zathura.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 160 insertions(+), 7 deletions(-) diff --git a/config.def.h b/config.def.h index 548cec2..bb54dd5 100644 --- a/config.def.h +++ b/config.def.h @@ -222,3 +222,26 @@ Setting settings[] = { {"zoom_min", &(zoom_min), 'f', FALSE, FALSE, "Zoom minimum"}, {"zoom_step", &(zoom_step), 'f', FALSE, FALSE, "Zoom step"}, }; + +/* shortcut names */ +ShortcutName shorcut_names[] = { + {"abort", sc_abort}, + {"adjust_window", sc_adjust_window}, + {"change_buffer", sc_change_buffer}, + {"change_mode", sc_change_mode}, + {"focus_inputbar", sc_focus_inputbar}, + {"follow", sc_follow}, + {"navigate", sc_navigate}, + {"navigate_index", sc_navigate_index}, + {"quit", sc_quit}, + {"recolor", sc_recolor}, + {"reload", sc_reload}, + {"rotate", sc_rotate}, + {"scroll", sc_scroll}, + {"search", sc_search}, + {"switch_goto_mode", sc_switch_goto_mode}, + {"toggle_fullscreen", sc_toggle_fullscreen}, + {"toggle_index", sc_toggle_index}, + {"toggle_inputbar", sc_toggle_inputbar}, + {"toggle_statusbar", sc_toggle_statusbar}, +}; diff --git a/zathura.c b/zathura.c index 8ff9ef5..5c02240 100644 --- a/zathura.c +++ b/zathura.c @@ -78,6 +78,12 @@ typedef struct Argument argument; } Shortcut; +typedef struct +{ + char* name; + void (*function)(Argument*); +} ShortcutName; + typedef struct { int mask; @@ -117,6 +123,14 @@ typedef struct Argument argument; } SpecialCommand; +struct SCList +{ + Shortcut element; + struct SCList *next; +}; + +typedef struct SCList ShortcutList; + typedef struct { PopplerPage *page; @@ -206,6 +220,11 @@ struct gboolean show_index; } Global; + struct + { + ShortcutList *sclist; + } Bindings; + struct { gdouble x; @@ -287,6 +306,7 @@ struct /* function declarations */ void init_look(); void init_directories(); +void init_keylist(); void init_settings(); void init_zathura(); void add_marker(int); @@ -300,6 +320,7 @@ void eval_marker(int); void notify(int, char*); gboolean open_file(char*, char*); void open_uri(char*); +void out_of_memory(); void update_status(); void read_configuration(); void recalcRectangle(int, PopplerRectangle*); @@ -474,6 +495,31 @@ init_directories() g_free(bookmarks); } +void +init_keylist() +{ + ShortcutList* e = NULL; + ShortcutList* p = NULL; + + int i; + for(i = 0; i < LENGTH(shortcuts); i++) + { + e = malloc(sizeof(ShortcutList)); + if(!e) + out_of_memory(); + + e->element = shortcuts[i]; + e->next = NULL; + + if(!Zathura.Bindings.sclist) + Zathura.Bindings.sclist = e; + if(p) + p->next = e; + + p = e; + } +} + void init_settings() { @@ -942,6 +988,9 @@ open_file(char* path, char* password) char* home_path = getenv("HOME"); int file_len = strlen(home_path) + strlen(path) - 1; file = malloc(file_len); + if(!file) + out_of_memory(); + snprintf(file, file_len, "%s%s", getenv("HOME"), path + 1); } @@ -1024,8 +1073,11 @@ open_file(char* path, char* password) Zathura.PDF.file = file; Zathura.PDF.scale = 100; Zathura.PDF.rotate = 0; - Zathura.PDF.pages = malloc(Zathura.PDF.number_of_pages * sizeof(Page*)); Zathura.State.filename = g_markup_escape_text(file, -1); + Zathura.PDF.pages = malloc(Zathura.PDF.number_of_pages * sizeof(Page*)); + + if(!Zathura.PDF.pages) + out_of_memory(); /* get pages and check label mode */ g_static_mutex_lock(&(Zathura.Lock.pdflib_lock)); @@ -1035,6 +1087,9 @@ open_file(char* path, char* password) for(i = 0; i < Zathura.PDF.number_of_pages; i++) { Zathura.PDF.pages[i] = malloc(sizeof(Page)); + if(!Zathura.PDF.pages[i]) + out_of_memory(); + Zathura.PDF.pages[i]->id = i + 1; Zathura.PDF.pages[i]->page = poppler_document_get_page(Zathura.PDF.document, i); g_object_get(G_OBJECT(Zathura.PDF.pages[i]->page), "label", &(Zathura.PDF.pages[i]->label), NULL); @@ -1106,6 +1161,12 @@ void open_uri(char* uri) g_free(uri_cmd); } +void out_of_memory() +{ + printf("error: out of memory\n"); + exit(-1); +} + void update_status() { @@ -2104,6 +2165,8 @@ isc_completion(Argument* argument) CompletionElement* element = NULL; rows = malloc(sizeof(CompletionRow)); + if(!rows) + out_of_memory(); for(group = result->groups; group != NULL; group = group->next) { @@ -2169,6 +2232,8 @@ isc_completion(Argument* argument) command_mode = TRUE; rows = malloc(LENGTH(commands) * sizeof(CompletionRow)); + if(!rows) + out_of_memory(); for(i = 0; i < LENGTH(commands); i++) { @@ -2558,6 +2623,9 @@ cmd_export(int argc, char** argv) { file = malloc(((int) strlen(filename) + (int) strlen(argv[1]) + (int) strlen(getenv("HOME")) - 1) * sizeof(char)); + if(!file) + out_of_memory(); + file = g_strdup_printf("%s%s%s", getenv("HOME"), argv[1] + 1, filename); } else @@ -2593,6 +2661,9 @@ cmd_export(int argc, char** argv) { file = malloc(((int) strlen(attachment->name) + (int) strlen(argv[1]) + (int) strlen(getenv("HOME")) - 1) * sizeof(char)); + if(!file) + out_of_memory(); + file = g_strdup_printf("%s%s%s", getenv("HOME"), argv[1] + 1, attachment->name); } else @@ -2858,7 +2929,12 @@ cc_bookmark(char* input) { /* init completion group */ Completion *completion = malloc(sizeof(Completion)); + if(!completion) + out_of_memory(); + CompletionGroup* group = malloc(sizeof(CompletionGroup)); + if(!group) + out_of_memory(); group->value = NULL; group->next = NULL; @@ -2876,6 +2952,9 @@ cc_bookmark(char* input) !strncmp(input, Zathura.Bookmarks.bookmarks[i].id, input_length) ) { CompletionElement* el = malloc(sizeof(CompletionElement)); + if(!el) + out_of_memory(); + el->value = Zathura.Bookmarks.bookmarks[i].id; el->description = g_strdup_printf("Page: %d", Zathura.Bookmarks.bookmarks[i].page); el->next = NULL; @@ -2897,7 +2976,12 @@ cc_export(char* input) { /* init completion group */ Completion *completion = malloc(sizeof(Completion)); + if(!completion) + out_of_memory(); + CompletionGroup* group = malloc(sizeof(CompletionGroup)); + if(!group) + out_of_memory(); group->value = NULL; group->next = NULL; @@ -2907,12 +2991,18 @@ cc_export(char* input) /* export images */ CompletionElement *export_images = malloc(sizeof(CompletionElement)); + if(!export_images) + out_of_memory(); + export_images->value = "images"; export_images->description = "Export images"; export_images->next = NULL; /* export attachmants */ CompletionElement *export_attachments = malloc(sizeof(CompletionElement)); + if(!export_attachments) + out_of_memory(); + export_attachments->value = "attachments"; export_attachments->description = "Export attachments"; export_attachments->next = NULL; @@ -2929,7 +3019,12 @@ cc_open(char* input) { /* init completion group */ Completion *completion = malloc(sizeof(Completion)); + if(!completion) + out_of_memory(); + CompletionGroup* group = malloc(sizeof(CompletionGroup)); + if(!group) + out_of_memory(); group->value = NULL; group->next = NULL; @@ -2994,6 +3089,9 @@ cc_open(char* input) (file_length == 0) ) { CompletionElement* el = malloc(sizeof(CompletionElement)); + if(!el) + out_of_memory(); + el->value = g_strdup_printf("%s%s", path, d_name); el->description = NULL; el->next = NULL; @@ -3017,7 +3115,12 @@ cc_print(char* input) { /* init completion group */ Completion *completion = malloc(sizeof(Completion)); + if(!completion) + out_of_memory(); + CompletionGroup* group = malloc(sizeof(CompletionGroup)); + if(!group) + out_of_memory(); group->value = NULL; group->next = NULL; @@ -3046,6 +3149,8 @@ cc_print(char* input) { if(!current_line) current_line = malloc(sizeof(char)); + if(!current_line) + out_of_memory(); current_line = realloc(current_line, (count + 1) * sizeof(char)); @@ -3060,6 +3165,9 @@ cc_print(char* input) (!strncmp(input, current_line, input_length)) ) { CompletionElement* el = malloc(sizeof(CompletionElement)); + if(!el) + out_of_memory(); + el->value = g_strdup(current_line); el->description = NULL; el->next = NULL; @@ -3088,7 +3196,12 @@ cc_set(char* input) { /* init completion group */ Completion *completion = malloc(sizeof(Completion)); + if(!completion) + out_of_memory(); + CompletionGroup* group = malloc(sizeof(CompletionGroup)); + if(!group) + out_of_memory(); group->value = NULL; group->next = NULL; @@ -3106,6 +3219,9 @@ cc_set(char* input) !strncmp(input, settings[i].name, input_length) ) { CompletionElement* el = malloc(sizeof(CompletionElement)); + if(!el) + out_of_memory(); + el->value = settings[i].name; el->description = settings[i].description; el->next = NULL; @@ -3249,6 +3365,16 @@ cb_destroy(GtkWidget* widget, gpointer data) g_list_free(Zathura.Global.history); + /* clean shortcut list */ + ShortcutList* sc = Zathura.Bindings.sclist; + + while(sc) + { + ShortcutList* ne = sc->next; + free(sc); + sc = ne; + } + gtk_main_quit(); return TRUE; @@ -3521,16 +3647,18 @@ cb_inputbar_password_activate(GtkEntry* entry, gpointer data) gboolean cb_view_kb_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data) { - int i; - for(i = 0; i < LENGTH(shortcuts); i++) + ShortcutList* sc = Zathura.Bindings.sclist; + while(sc) { - if (event->keyval == shortcuts[i].key && - (((event->state & shortcuts[i].mask) == shortcuts[i].mask) || shortcuts[i].mask == 0) - && (Zathura.Global.mode == shortcuts[i].mode || shortcuts[i].mode == -1)) + if (event->keyval == sc->element.key && + (((event->state & sc->element.mask) == sc->element.mask) || sc->element.mask == 0) + && (Zathura.Global.mode == sc->element.mode || sc->element.mode == -1)) { - shortcuts[i].function(&(shortcuts[i].argument)); + sc->element.function(&(sc->element.argument)); return TRUE; } + + sc = sc->next; } if(Zathura.Global.mode == ADD_MARKER) @@ -3559,6 +3687,7 @@ cb_view_kb_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data) /* search buffer commands */ if(Zathura.Global.buffer) { + int i; for(i = 0; i < LENGTH(buffer_commands); i++) { regex_t regex; @@ -3760,6 +3889,7 @@ int main(int argc, char* argv[]) init_zathura(); read_configuration(); init_settings(); + init_keylist(); init_look(); init_directories(); From c6a9d72f36d7dfcc293df433bb55060df3a9c42a Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 16 Jun 2010 23:13:25 +0200 Subject: [PATCH 6/7] Simple map function without any parsing --- config.def.h | 2 +- zathura.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index bb54dd5..2c38264 100644 --- a/config.def.h +++ b/config.def.h @@ -224,7 +224,7 @@ Setting settings[] = { }; /* shortcut names */ -ShortcutName shorcut_names[] = { +ShortcutName shortcut_names[] = { {"abort", sc_abort}, {"adjust_window", sc_adjust_window}, {"change_buffer", sc_change_buffer}, diff --git a/zathura.c b/zathura.c index 5c02240..b1ec431 100644 --- a/zathura.c +++ b/zathura.c @@ -2756,7 +2756,53 @@ gboolean cmd_map(int argc, char** argv) { if(argc < 2) + return TRUE; + + /* search for the right shortcut function */ + int sc_id = -1; + + int sc_c; + for(sc_c = 0; sc_c < LENGTH(shortcut_names); sc_c++) + { + if(!strcmp(argv[1], shortcut_names[sc_c].name)) + { + sc_id = sc_c; + break; + } + } + + if(sc_id == -1) + { + notify(WARNING, "No such shortcut function exists"); return FALSE; + } + + /* search for existing binding */ + ShortcutList* sc = Zathura.Bindings.sclist; + while(sc && sc->next != NULL) + { + sc = sc->next; + } + + /* create new entry */ + ShortcutList* entry = malloc(sizeof(ShortcutList)); + if(!entry) + out_of_memory(); + + Argument arg; + entry->element.mask = 0; + entry->element.key = GDK_y; + entry->element.function = shortcut_names[sc_id].function; + entry->element.mode = -1; + entry->element.argument = arg; + entry->next = NULL; + + /* append to list */ + if(!Zathura.Bindings.sclist) + Zathura.Bindings.sclist = entry; + + if(sc) + sc->next = entry; return TRUE; } From 58d42bf3294b41e0e7124e4d1b6cd03a74a08af7 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 17 Jun 2010 00:12:21 +0200 Subject: [PATCH 7/7] Arguments and mode for mappings It is now possible to define an argument and an additional mode for the mapping. --- config.def.h | 36 ++++++++++++++++ zathura.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 146 insertions(+), 8 deletions(-) diff --git a/config.def.h b/config.def.h index 2c38264..d75b16e 100644 --- a/config.def.h +++ b/config.def.h @@ -245,3 +245,39 @@ ShortcutName shortcut_names[] = { {"toggle_inputbar", sc_toggle_inputbar}, {"toggle_statusbar", sc_toggle_statusbar}, }; + +/* argument names */ +ArgumentName argument_names[] = { + {"add_marker", ADD_MARKER}, + {"backward", BACKWARD}, + {"bestfit", ADJUST_BESTFIT}, + {"collapse", COLLAPSE}, + {"delete_last", DELETE_LAST}, + {"down", DOWN}, + {"eval_marker", EVAL_MARKER}, + {"expand", EXPAND}, + {"forward", FORWARD}, + {"full_down", FULL_DOWN}, + {"full_up", FULL_UP}, + {"half_down", HALF_DOWN}, + {"half_up", HALF_UP}, + {"insert", INSERT}, + {"left", LEFT}, + {"next", NEXT}, + {"previous", PREVIOUS}, + {"right", RIGHT}, + {"select", SELECT}, + {"up", UP}, + {"visual", VISUAL}, + {"width", ADJUST_WIDTH}, +}; + +/* mode names */ +ModeName mode_names[] = { + {"all", -1}, + {"fullscreen", FULLSCREEN}, + {"index", INDEX}, + {"insert", INSERT}, + {"normal", NORMAL}, + {"visual", VISUAL}, +}; diff --git a/zathura.c b/zathura.c index b1ec431..518a0b7 100644 --- a/zathura.c +++ b/zathura.c @@ -69,6 +69,12 @@ typedef struct void *data; } Argument; +typedef struct +{ + char* name; + int argument; +} ArgumentName; + typedef struct { int mask; @@ -78,6 +84,12 @@ typedef struct Argument argument; } Shortcut; +typedef struct +{ + char* name; + int mode; +} ModeName; + typedef struct { char* name; @@ -1184,7 +1196,7 @@ update_status() /* update state */ char* zoom_level = (Zathura.PDF.scale != 0) ? g_strdup_printf("%d%%", Zathura.PDF.scale) : g_strdup(""); - char* goto_mode = (Zathura.Global.goto_mode == GOTO_LABELS) ? "L" : + char* goto_mode = (Zathura.Global.goto_mode == GOTO_LABELS) ? "L" : (Zathura.Global.goto_mode == GOTO_OFFSET) ? "O" : "D"; char* status_text = g_strdup_printf("%s [%s] %s", zoom_level, goto_mode, Zathura.State.pages); gtk_label_set_markup((GtkLabel*) Zathura.Global.status_state, status_text); @@ -1217,6 +1229,8 @@ read_configuration() if(!strcmp(tokens[0], "set")) cmd_set(length - 1, tokens + 1); + else if(!strcmp(tokens[0], "map")) + cmd_map(length - 1, tokens + 1); } } } @@ -2758,6 +2772,8 @@ cmd_map(int argc, char** argv) if(argc < 2) return TRUE; + char* ks = argv[0]; + /* search for the right shortcut function */ int sc_id = -1; @@ -2777,10 +2793,97 @@ cmd_map(int argc, char** argv) return FALSE; } - /* search for existing binding */ + /* parse modifier and key */ + int mask = 0; + int key = 0; + int mode = NORMAL; + + // single key (e.g.: g) + if(strlen(ks) == 1) + key = ks[0]; + + // modifier and key (e.g.: + if(strlen(ks) == 5 && ks[0] == '<' && ks[2] == '-' && ks[4] == '>') + { + /* evaluate modifier */ + switch(ks[1]) + { + case 'S': + mask = GDK_SHIFT_MASK; + break; + case 'C': + mask = GDK_CONTROL_MASK; + break; + } + + /* get key */ + key = ks[3]; + + /* no valid modifier */ + if(!mask) + { + notify(WARNING, "No valid modifier given."); + return FALSE; + } + } + + /* parse argument */ + Argument arg = {0, 0}; + + if(argc >= 3) + { + int arg_id = -1; + + /* compare argument with given argument names... */ + int arg_c; + for(arg_c = 0; arg_c < LENGTH(argument_names); arg_c++) + { + if(!strcmp(argv[2], argument_names[arg_c].name)) + { + arg_id = argument_names[arg_c].argument; + break; + } + } + + /* if not, save it do .data */ + if(arg_id == -1) + arg.data = argv[2]; + else + arg.n = arg_id; + } + + /* parse mode */ + if(argc >= 4) + { + int mode_c; + for(mode_c = 0; mode_c < LENGTH(mode_names); mode_c++) + { + if(!strcmp(argv[3], mode_names[mode_c].name)) + { + mode = mode_names[mode_c].mode; + break; + } + } + } + + if(!key) + { + notify(WARNING, "No valid key binding given."); + return FALSE; + } + + /* search for existing binding to overwrite it */ ShortcutList* sc = Zathura.Bindings.sclist; while(sc && sc->next != NULL) { + if(sc->element.key == key && sc->element.mask == mask + && sc->element.mode == mode) + { + sc->element.function = shortcut_names[sc_id].function; + sc->element.argument = arg; + return TRUE; + } + sc = sc->next; } @@ -2789,13 +2892,12 @@ cmd_map(int argc, char** argv) if(!entry) out_of_memory(); - Argument arg; - entry->element.mask = 0; - entry->element.key = GDK_y; + entry->element.mask = mask; + entry->element.key = key; entry->element.function = shortcut_names[sc_id].function; - entry->element.mode = -1; + entry->element.mode = mode; entry->element.argument = arg; - entry->next = NULL; + entry->next = NULL; /* append to list */ if(!Zathura.Bindings.sclist) @@ -3933,9 +4035,9 @@ int main(int argc, char* argv[]) gtk_init(&argc, &argv); init_zathura(); + init_keylist(); read_configuration(); init_settings(); - init_keylist(); init_look(); init_directories();