From c661506681077c811e96483cafd93fbf50c1bf77 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Tue, 16 Aug 2016 10:49:55 +0200 Subject: [PATCH] tweaks: use a separate function to ask ncurses for keycodes --- src/global.c | 5 +---- src/nano.c | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/global.c b/src/global.c index 3347476f..da404d27 100644 --- a/src/global.c +++ b/src/global.c @@ -47,10 +47,7 @@ message_type lastmessage = HUSH; /* Messages of type HUSH should not overwrite type MILD nor ALERT. */ #ifndef NANO_TINY -int controlleft = CONTROL_LEFT; -int controlright = CONTROL_RIGHT; -int controlup = CONTROL_UP; -int controldown = CONTROL_DOWN; +int controlleft, controlright, controlup, controldown; #endif #ifndef DISABLE_WRAPJUSTIFY diff --git a/src/nano.c b/src/nano.c index 8458a475..356a1de3 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1535,6 +1535,19 @@ void terminal_init(void) #endif } +#if !defined(NANO_TINY) && defined(HAVE_KEY_DEFINED) +/* Ask ncurses for a keycode, or assign a default one. */ +int get_keycode(const char *keyname, const int standard) +{ + const char *keyvalue = tigetstr(keyname); + + if (keyvalue == 0 || keyvalue == (char *)-1) + return standard; + else + return key_defined(keyvalue); +} +#endif + /* Say that an unbound key was struck, and if possible which one. */ void unbound_key(int code) { @@ -2514,20 +2527,11 @@ int main(int argc, char **argv) #endif #if !defined(NANO_TINY) && defined(HAVE_KEY_DEFINED) - const char *keyvalue; /* Ask ncurses for the key codes for Control+Left/Right/Up/Down. */ - keyvalue = tigetstr("kLFT5"); - if (keyvalue != 0 && keyvalue != (char *)-1) - controlleft = key_defined(keyvalue); - keyvalue = tigetstr("kRIT5"); - if (keyvalue != 0 && keyvalue != (char *)-1) - controlright = key_defined(keyvalue); - keyvalue = tigetstr("kUP5"); - if (keyvalue != 0 && keyvalue != (char *)-1) - controlup = key_defined(keyvalue); - keyvalue = tigetstr("kDN5"); - if (keyvalue != 0 && keyvalue != (char *)-1) - controldown = key_defined(keyvalue); + controlleft = get_keycode("kLFT5", CONTROL_LEFT); + controlright = get_keycode("kRIT5", CONTROL_RIGHT); + controlup = get_keycode("kUP5", CONTROL_UP); + controldown = get_keycode("kDN5", CONTROL_DOWN); #endif #ifndef USE_SLANG