Krolyxon
Commit 54d7b7f

Branches

Refs →

nsxiv54d7b7f

Use a prefix key to execute the key handler

The default prefix key is C-x, and can be changed in config.def.h. The
first key pressed after the prefix key will be passed the external key
handler, unless the key is Escape, which is used to cancel the prefix.
Author
András Mohari
Date
2014-01-31 14:17 +0100
Parent
d5b2d37
Commit
54d7b7f20fc80460fb6160badb6d90e6505a4194

+25 −5

4 files changed

  1. M README.md +4 −0
  2. M config.def.h +6 −0
  3. M exec/key-handler +3 −3
  4. M main.c +12 −2
M README.md View
@@ -113,6 +113,10 @@ of small previews is displayed, making it easy to choose an image to open.
113113 N Go [count] marked images forward
114114 P Go [count] marked images backward
115115
116+ Ctrl-x KEY Execute $XDG_CONFIG_HOME/sxiv/exec/key-handler with
117+ KEY and the path of the current image as arguments.
118+ If KEY is Escape, it cancels the effect of Ctrl-x.
119+
116120 *Thumbnail mode:*
117121
118122 h,j,k,l Move selection left/down/up/right [count] times
M config.def.h View
@@ -77,6 +77,12 @@ static const bool RENDER_WHITE_ALPHA = false;
7777 #endif
7878 #ifdef _MAPPINGS_CONFIG
7979
80+/* the prefix key: when pressed, the next key combo is passed to the external
81+ * key handler
82+ */
83+#define PREFIX_KEYMASK ControlMask
84+#define PREFIX_KEYSYM XK_x
85+
8086 /* keyboard mappings for image and thumbnail mode: */
8187 static const keymap_t keys[] = {
8288 /* modifiers key function argument */
M exec/key-handler View
@@ -1,9 +1,9 @@
11 #!/bin/sh
22
33 # Example for $XDG_CONFIG_HOME/sxiv/exec/key-handler
4−# Called by sxiv(1) whenever an unbound key combo is used,
5−# with the key combo as its first argument and the path of the current image
6−# as its second argument.
4+# Called by sxiv(1) when you press the prefix key (C-x by default) followed by
5+# a key combo. The key combo is passed as its first argument and the path of
6+# the current image as its second argument.
77 # sxiv(1) blocks until this script terminates. It then checks if the image
88 # has been modified and reloads it.
99
M main.c View
@@ -504,6 +504,7 @@ void run_key_handler(const char *key, unsigned int mask)
504504
505505 void on_keypress(XKeyEvent *kev)
506506 {
507+ static bool seen_prefix_key = false;
507508 int i;
508509 unsigned int sh;
509510 KeySym ksym, shksym;
@@ -523,6 +524,17 @@ void on_keypress(XKeyEvent *kev)
523524 if (IsModifierKey(ksym))
524525 return;
525526
527+ if (seen_prefix_key) {
528+ seen_prefix_key = false;
529+ if (!(MODMASK(kev->state) == 0 && ksym == XK_Escape))
530+ run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
531+ return;
532+ } else if (MODMASK(kev->state) == PREFIX_KEYMASK && ksym == PREFIX_KEYSYM) {
533+ seen_prefix_key = true;
534+ prefix = 0;
535+ return;
536+ }
537+
526538 if ((ksym == XK_Escape && MODMASK(kev->state) == 0) ||
527539 (key >= '0' && key <= '9'))
528540 {
@@ -545,8 +557,6 @@ void on_keypress(XKeyEvent *kev)
545557 break;
546558 }
547559 }
548− if (i == ARRLEN(keys))
549− run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
550560 prefix = 0;
551561 }
552562