Move the -w switch to the configuration file

Update README accordingly.

Woo, I get to remove code.
This commit is contained in:
Přemysl Eric Janouch 2016-09-28 05:50:03 +02:00
parent b3458d84a0
commit 38b463c883
Signed by: p
GPG Key ID: B715679E3A361BE6
2 changed files with 43 additions and 65 deletions

View File

@ -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. `fakeroot` or file ownership will end up wrong.
Having the program installed, simply run it with a StarDict '.ifo' file as an 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 argument. It is however highly recommended to configure it, see below.
changes and automatically search for the selected text, use the `-w` switch.
This feature requires GTK+.
Extensions Extensions
---------- ----------
@ -78,14 +76,28 @@ with the following. Note that it is intended for black-on-white terminals.
center-search = true center-search = true
underline-last = false underline-last = false
hl-common-prefix = true hl-common-prefix = true
watch-selection = true
[Colors] [Colors]
header = reverse header = reverse
header-active = underline
search = ul search = ul
even = 16 231 even = 16 231
odd = 16 255 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 Dictionaries
------------ ------------
Unfortunately this application only really works with specific dictionaries. Unfortunately this application only really works with specific dictionaries.

View File

@ -184,8 +184,6 @@ typedef struct view_entry ViewEntry;
typedef struct dictionary Dictionary; typedef struct dictionary Dictionary;
/// Encloses application data. /// Encloses application data.
typedef struct application Application; typedef struct application Application;
/// Application options.
typedef struct app_options AppOptions;
struct view_entry struct view_entry
{ {
@ -240,12 +238,6 @@ struct application
/// Shortcut to retrieve named terminal attributes /// Shortcut to retrieve named terminal attributes
#define APP_ATTR(name) self->attrs[ATTRIBUTE_ ## name].attrs #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. /// 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 = self->hl_prefix =
app_load_bool (kf, "hl-common-prefix", 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_) \ #define XX(name, config, fg_, bg_, attrs_) \
app_load_color (self, kf, config, ATTRIBUTE_ ## name); app_load_color (self, kf, config, ATTRIBUTE_ ## name);
ATTRIBUTE_TABLE (XX) ATTRIBUTE_TABLE (XX)
@ -524,25 +524,13 @@ app_init_attrs (Application *self)
/// Initialize the application core. /// Initialize the application core.
static void static void
app_init (Application *self, AppOptions *options, char **filenames) app_init (Application *self, char **filenames)
{ {
self->loop = NULL; self->loop = NULL;
self->selection_interval = options->selection_watcher; self->selection_interval = -1;
self->selection_timer = 0; self->selection_timer = 0;
self->selection_contents = NULL; 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 = NULL;
self->tk_timer = 0; self->tk_timer = 0;
@ -586,6 +574,19 @@ app_init (Application *self, AppOptions *options, char **filenames)
exit (EXIT_FAILURE); 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 // Dictionaries given on the command line override the configuration
if (*filenames) if (*filenames)
{ {
@ -1899,29 +1900,6 @@ on_selection_timer (gpointer data)
app->selection_timer = 0; app->selection_timer = 0;
return FALSE; 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 #endif // WITH_GTK
static void static void
@ -1990,24 +1968,12 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_type_init (); g_type_init ();
G_GNUC_END_IGNORE_DEPRECATIONS G_GNUC_END_IGNORE_DEPRECATIONS
AppOptions options = gboolean show_version = FALSE;
{
.show_version = FALSE,
.selection_watcher = -1,
};
GOptionEntry entries[] = GOptionEntry entries[] =
{ {
{ "version", 0, G_OPTION_FLAG_IN_MAIN, { "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 }, 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 } { NULL }
}; };
@ -2021,7 +1987,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
GError *error = NULL; GError *error = NULL;
GOptionContext *ctx = g_option_context_new GOptionContext *ctx = g_option_context_new
(N_("[dictionary.ifo]... - StarDict terminal UI")); (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_add_entries (group, entries);
g_option_group_set_translation_domain (group, GETTEXT_PACKAGE); g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
g_option_context_add_group (ctx, group); g_option_context_add_group (ctx, group);
@ -2034,14 +2000,14 @@ G_GNUC_END_IGNORE_DEPRECATIONS
} }
g_option_context_free (ctx); g_option_context_free (ctx);
if (options.show_version) if (show_version)
{ {
g_print (PROJECT_NAME " " PROJECT_VERSION "\n"); g_print (PROJECT_NAME " " PROJECT_VERSION "\n");
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
Application app; Application app;
app_init (&app, &options, argv + 1); app_init (&app, argv + 1);
app_init_terminal (&app); app_init_terminal (&app);
app_redraw (&app); app_redraw (&app);