summaryrefslogtreecommitdiff
path: root/src/keys.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2015-02-24 11:13:38 +0100
committerLukas Fleischer <calcurse@cryptocrack.de>2015-02-24 11:17:38 +0100
commit52779d2ec6391a122cb2822c147d8c1f86e8b6ba (patch)
tree3d095b059dda5dd8763685b4f609b472676d825d /src/keys.c
parentf91208c2f43d6fda12f410840a0a1cd63091a9d3 (diff)
downloadcalcurse-52779d2ec6391a122cb2822c147d8c1f86e8b6ba.zip
Introduce starts_with() and starts_with_ci()
Create user-defined functions to check whether a string contains a certain prefix instead of messing around with strncmp() and strncasecmp(). Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/keys.c')
-rw-r--r--src/keys.c66
1 files changed, 27 insertions, 39 deletions
diff --git a/src/keys.c b/src/keys.c
index ef81120..690dc5a 100644
--- a/src/keys.c
+++ b/src/keys.c
@@ -275,48 +275,36 @@ void keys_remove_binding(int key, enum key action)
int keys_str2int(const char *key)
{
- const char CONTROL_KEY[] = "C-";
- const char TAB_KEY[] = "TAB";
- const char SPACE_KEY[] = "SPC";
- const char ESCAPE_KEY[] = "ESC";
- const char CURSES_KEY_UP[] = "UP";
- const char CURSES_KEY_DOWN[] = "DWN";
- const char CURSES_KEY_LEFT[] = "LFT";
- const char CURSES_KEY_RIGHT[] = "RGT";
- const char CURSES_KEY_HOME[] = "KEY_HOME";
- const char CURSES_KEY_END[] = "KEY_END";
-
if (!key)
return -1;
- if (strlen(key) == 1) {
+
+ if (strlen(key) == 1)
return (int)key[0];
- } else {
- if (key[0] == '^')
- return CTRL((int)key[1]);
- else if (!strncmp
- (key, CONTROL_KEY, sizeof(CONTROL_KEY) - 1))
- return CTRL((int)key[sizeof(CONTROL_KEY) - 1]);
- else if (!strcmp(key, TAB_KEY))
- return TAB;
- else if (!strcmp(key, ESCAPE_KEY))
- return ESCAPE;
- else if (!strcmp(key, SPACE_KEY))
- return SPACE;
- else if (!strcmp(key, CURSES_KEY_UP))
- return KEY_UP;
- else if (!strcmp(key, CURSES_KEY_DOWN))
- return KEY_DOWN;
- else if (!strcmp(key, CURSES_KEY_LEFT))
- return KEY_LEFT;
- else if (!strcmp(key, CURSES_KEY_RIGHT))
- return KEY_RIGHT;
- else if (!strcmp(key, CURSES_KEY_HOME))
- return KEY_HOME;
- else if (!strcmp(key, CURSES_KEY_END))
- return KEY_END;
- else
- return -1;
- }
+
+ if (key[0] == '^')
+ return CTRL((int)key[1]);
+ else if (starts_with(key, "C-"))
+ return CTRL((int)key[strlen("C-")]);
+ else if (!strcmp(key, "TAB"))
+ return TAB;
+ else if (!strcmp(key, "ESC"))
+ return ESCAPE;
+ else if (!strcmp(key, "SPC"))
+ return SPACE;
+ else if (!strcmp(key, "UP"))
+ return KEY_UP;
+ else if (!strcmp(key, "DWN"))
+ return KEY_DOWN;
+ else if (!strcmp(key, "LFT"))
+ return KEY_LEFT;
+ else if (!strcmp(key, "RGT"))
+ return KEY_RIGHT;
+ else if (!strcmp(key, "KEY_HOME"))
+ return KEY_HOME;
+ else if (!strcmp(key, "KEY_END"))
+ return KEY_END;
+
+ return -1;
}
const char *keys_int2str(int key)