+#ifdef NCURSES_VERSION
+struct extkey {
+ const char *name;
+ const char *sym;
+};
+
+static const struct extkey ExtKeys[] = {
+ { "<c-up>", "kUP5" },
+ { "<s-up>", "kUP" },
+ { "<a-up>", "kUP3" },
+
+ { "<s-down>", "kDN" },
+ { "<a-down>", "kDN3" },
+ { "<c-down>", "kDN5" },
+
+ { "<c-right>", "kRIT5" },
+ { "<s-right>", "kRIT" },
+ { "<a-right>", "kRIT3" },
+
+ { "<s-left>", "kLFT" },
+ { "<a-left>", "kLFT3" },
+ { "<c-left>", "kLFT5" },
+
+ { "<s-home>", "kHOM" },
+ { "<a-home>", "kHOM3" },
+ { "<c-home>", "kHOM5" },
+
+ { "<s-end>", "kEND" },
+ { "<a-end>", "kEND3" },
+ { "<c-end>", "kEND5" },
+
+ { "<s-next>", "kNXT" },
+ { "<a-next>", "kNXT3" },
+ { "<c-next>", "kNXT5" },
+
+ { "<s-prev>", "kPRV" },
+ { "<a-prev>", "kPRV3" },
+ { "<c-prev>", "kPRV5" },
+
+ { 0, 0 }
+};
+
+/* Look up Mutt's name for a key and find the ncurses extended name for it */
+static const char *find_ext_name(const char *key)
+{
+ int j;
+
+ for (j = 0; ExtKeys[j].name; ++j)
+ {
+ if (strcasecmp(key, ExtKeys[j].name) == 0)
+ return ExtKeys[j].sym;
+ }
+ return 0;
+}
+#endif /* NCURSES_VERSION */
+
+/* Determine the keycodes for ncurses extended keys and fill in the KeyNames array.
+ *
+ * This function must be called *after* initscr(), or tigetstr() returns -1. This
+ * creates a bit of a chicken-and-egg problem because km_init() is called prior to
+ * start_curses(). This means that the default keybindings can't include any of the
+ * extended keys because they won't be defined until later.
+ */
+void init_extended_keys(void)
+{
+#ifdef NCURSES_VERSION
+ int j;
+
+ use_extended_names(TRUE);
+
+ for (j = 0; KeyNames[j].name; ++j)
+ {
+ if (KeyNames[j].value == -1)
+ {
+ const char *keyname = find_ext_name(KeyNames[j].name);
+
+ if (keyname)
+ {
+ char *s = tigetstr(keyname);
+ if (s && (long)(s) != -1)
+ {
+ int code = key_defined(s);
+ if (code > 0)
+ KeyNames[j].value = code;
+ }
+ }
+ }
+ }
+#endif
+}
+