Arguments and mode for mappings

It is now possible to define an argument and an additional
mode for the mapping.
This commit is contained in:
Moritz Lipp 2010-06-17 00:12:21 +02:00
parent 31d10fa0f0
commit 8df70f0e83
2 changed files with 146 additions and 8 deletions

View file

@ -245,3 +245,39 @@ ShortcutName shortcut_names[] = {
{"toggle_inputbar", sc_toggle_inputbar}, {"toggle_inputbar", sc_toggle_inputbar},
{"toggle_statusbar", sc_toggle_statusbar}, {"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},
};

118
zathura.c
View file

@ -69,6 +69,12 @@ typedef struct
void *data; void *data;
} Argument; } Argument;
typedef struct
{
char* name;
int argument;
} ArgumentName;
typedef struct typedef struct
{ {
int mask; int mask;
@ -78,6 +84,12 @@ typedef struct
Argument argument; Argument argument;
} Shortcut; } Shortcut;
typedef struct
{
char* name;
int mode;
} ModeName;
typedef struct typedef struct
{ {
char* name; char* name;
@ -1184,7 +1196,7 @@ update_status()
/* update state */ /* update state */
char* zoom_level = (Zathura.PDF.scale != 0) ? g_strdup_printf("%d%%", Zathura.PDF.scale) : g_strdup(""); 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"; (Zathura.Global.goto_mode == GOTO_OFFSET) ? "O" : "D";
char* status_text = g_strdup_printf("%s [%s] %s", zoom_level, goto_mode, Zathura.State.pages); 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); gtk_label_set_markup((GtkLabel*) Zathura.Global.status_state, status_text);
@ -1217,6 +1229,8 @@ read_configuration()
if(!strcmp(tokens[0], "set")) if(!strcmp(tokens[0], "set"))
cmd_set(length - 1, tokens + 1); 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) if(argc < 2)
return TRUE; return TRUE;
char* ks = argv[0];
/* search for the right shortcut function */ /* search for the right shortcut function */
int sc_id = -1; int sc_id = -1;
@ -2777,10 +2793,97 @@ cmd_map(int argc, char** argv)
return FALSE; 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.: <S-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; ShortcutList* sc = Zathura.Bindings.sclist;
while(sc && sc->next != NULL) 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; sc = sc->next;
} }
@ -2789,13 +2892,12 @@ cmd_map(int argc, char** argv)
if(!entry) if(!entry)
out_of_memory(); out_of_memory();
Argument arg; entry->element.mask = mask;
entry->element.mask = 0; entry->element.key = key;
entry->element.key = GDK_y;
entry->element.function = shortcut_names[sc_id].function; entry->element.function = shortcut_names[sc_id].function;
entry->element.mode = -1; entry->element.mode = mode;
entry->element.argument = arg; entry->element.argument = arg;
entry->next = NULL; entry->next = NULL;
/* append to list */ /* append to list */
if(!Zathura.Bindings.sclist) if(!Zathura.Bindings.sclist)
@ -3933,9 +4035,9 @@ int main(int argc, char* argv[])
gtk_init(&argc, &argv); gtk_init(&argc, &argv);
init_zathura(); init_zathura();
init_keylist();
read_configuration(); read_configuration();
init_settings(); init_settings();
init_keylist();
init_look(); init_look();
init_directories(); init_directories();