From 38b463c8833231e155c2a0b9ae741dac7a306e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Wed, 28 Sep 2016 05:50:03 +0200 Subject: [PATCH] Move the -w switch to the configuration file Update README accordingly. Woo, I get to remove code. --- README.adoc | 18 +++++++++-- src/sdtui.c | 90 +++++++++++++++++------------------------------------ 2 files changed, 43 insertions(+), 65 deletions(-) diff --git a/README.adoc b/README.adoc index 78f9103..77c6cfb 100644 --- a/README.adoc +++ b/README.adoc @@ -56,9 +56,7 @@ Note that for versions of CMake before 2.8.9, you need to prefix `cpack` with `fakeroot` or file ownership will end up wrong. Having the program installed, simply run it with a StarDict '.ifo' file as an -argument. If you want the application to watch the X11 primary selection for -changes and automatically search for the selected text, use the `-w` switch. -This feature requires GTK+. +argument. It is however highly recommended to configure it, see below. Extensions ---------- @@ -78,14 +76,28 @@ with the following. Note that it is intended for black-on-white terminals. center-search = true underline-last = false hl-common-prefix = true +watch-selection = true [Colors] header = reverse +header-active = underline search = ul even = 16 231 odd = 16 255 .... +The `watch-selection` option makes the application watch the X11 primary +selection for changes and automatically search for selected text. +This feature requires GTK+ and it will never work on Wayland by its design. + +You can also set up some dictionaries to be loaded at startup automatically: + +.... +[Dictionaries] +name1 = ~/path/to/dict.ifo +name2 = ~/another/dict.ifo +.... + Dictionaries ------------ Unfortunately this application only really works with specific dictionaries. diff --git a/src/sdtui.c b/src/sdtui.c index 05b161c..6c4acbb 100644 --- a/src/sdtui.c +++ b/src/sdtui.c @@ -184,8 +184,6 @@ typedef struct view_entry ViewEntry; typedef struct dictionary Dictionary; /// Encloses application data. typedef struct application Application; -/// Application options. -typedef struct app_options AppOptions; struct view_entry { @@ -240,12 +238,6 @@ struct application /// Shortcut to retrieve named terminal attributes #define APP_ATTR(name) self->attrs[ATTRIBUTE_ ## name].attrs -struct app_options -{ - gboolean show_version; ///< Output version information and quit - gint selection_watcher; ///< Interval in milliseconds, or -1 -}; - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /// Splits the entry and adds it to a pointer array. @@ -454,6 +446,14 @@ app_load_config_values (Application *self, GKeyFile *kf) self->hl_prefix = app_load_bool (kf, "hl-common-prefix", self->hl_prefix); + guint64 timer; + const gchar *watch_selection = "watch-selection"; + if (app_load_bool (kf, watch_selection, FALSE)) + self->selection_interval = 500; + else if ((timer = g_key_file_get_uint64 + (kf, "Settings", watch_selection, NULL)) && timer <= G_MAXINT) + self->selection_interval = timer; + #define XX(name, config, fg_, bg_, attrs_) \ app_load_color (self, kf, config, ATTRIBUTE_ ## name); ATTRIBUTE_TABLE (XX) @@ -524,25 +524,13 @@ app_init_attrs (Application *self) /// Initialize the application core. static void -app_init (Application *self, AppOptions *options, char **filenames) +app_init (Application *self, char **filenames) { self->loop = NULL; - self->selection_interval = options->selection_watcher; + self->selection_interval = -1; self->selection_timer = 0; self->selection_contents = NULL; -#ifdef WITH_GTK - if (gtk_init_check (0, NULL)) - { - // So that we set the input only when it actually changes - GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY); - self->selection_contents = gtk_clipboard_wait_for_text (clipboard); - rearm_selection_watcher (self); - } - else -#endif // WITH_GTK - self->loop = g_main_loop_new (NULL, FALSE); - self->tk = NULL; self->tk_timer = 0; @@ -586,6 +574,19 @@ app_init (Application *self, AppOptions *options, char **filenames) exit (EXIT_FAILURE); } + // Now we have settings for the clipboard watcher, we can arm the timer +#ifdef WITH_GTK + if (gtk_init_check (0, NULL)) + { + // So that we set the input only when it actually changes + GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY); + self->selection_contents = gtk_clipboard_wait_for_text (clipboard); + rearm_selection_watcher (self); + } + else +#endif // WITH_GTK + self->loop = g_main_loop_new (NULL, FALSE); + // Dictionaries given on the command line override the configuration if (*filenames) { @@ -1899,29 +1900,6 @@ on_selection_timer (gpointer data) app->selection_timer = 0; return FALSE; } - -static gboolean -on_watch_primary_selection (G_GNUC_UNUSED const gchar *option_name, - const gchar *value, gpointer data, GError **error) -{ - AppOptions *options = data; - - if (!value) - { - options->selection_watcher = 500; - return TRUE; - } - - unsigned long timer; - if (!xstrtoul (&timer, value, 10) || !timer || timer > G_MAXINT) - { - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, - _("Invalid timer value")); - return FALSE; - } - options->selection_watcher = timer; - return TRUE; -} #endif // WITH_GTK static void @@ -1990,24 +1968,12 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS g_type_init (); G_GNUC_END_IGNORE_DEPRECATIONS - AppOptions options = - { - .show_version = FALSE, - .selection_watcher = -1, - }; - + gboolean show_version = FALSE; GOptionEntry entries[] = { { "version", 0, G_OPTION_FLAG_IN_MAIN, - G_OPTION_ARG_NONE, &options.show_version, + G_OPTION_ARG_NONE, &show_version, N_("Output version information and exit"), NULL }, -#ifdef WITH_GTK - { "watch-primary-selection", 'w', - G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_OPTIONAL_ARG, - G_OPTION_ARG_CALLBACK, (gpointer) on_watch_primary_selection, - N_("Watch the value of the primary selection for input"), - N_("TIMER") }, -#endif // WITH_GTK { NULL } }; @@ -2021,7 +1987,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS GError *error = NULL; GOptionContext *ctx = g_option_context_new (N_("[dictionary.ifo]... - StarDict terminal UI")); - GOptionGroup *group = g_option_group_new ("", "", "", &options, NULL); + GOptionGroup *group = g_option_group_new ("", "", "", NULL, NULL); g_option_group_add_entries (group, entries); g_option_group_set_translation_domain (group, GETTEXT_PACKAGE); g_option_context_add_group (ctx, group); @@ -2034,14 +2000,14 @@ G_GNUC_END_IGNORE_DEPRECATIONS } g_option_context_free (ctx); - if (options.show_version) + if (show_version) { g_print (PROJECT_NAME " " PROJECT_VERSION "\n"); exit (EXIT_SUCCESS); } Application app; - app_init (&app, &options, argv + 1); + app_init (&app, argv + 1); app_init_terminal (&app); app_redraw (&app);