]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/key_defined.c
ncurses 5.4
[ncurses.git] / ncurses / base / key_defined.c
similarity index 68%
rename from ncurses/widechar/lib_ins_nwstr.c
rename to ncurses/base/key_defined.c
index 4b737713d4f8805bb6c82df22369561cac4a0c25..dac188748770e717fd40b6bd6fe91269415e39f8 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2002 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2003 Free Software Foundation, Inc.                        *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
  ****************************************************************************/
 
 /****************************************************************************
- * Author: Thomas Dickey 2002                                               *
+ *  Author: Thomas E. Dickey, 2003                                          *
  ****************************************************************************/
 
-/*
-**     lib_ins_nwstr.c
-**
-**     The routine wins_nwstr().
-**
-*/
-
 #include <curses.priv.h>
-#include <ctype.h>
 
-MODULE_ID("$Id: lib_ins_nwstr.c,v 1.3 2002/09/28 16:31:33 tom Exp $")
+MODULE_ID("$Id: key_defined.c,v 1.3 2003/05/17 23:12:27 tom Exp $")
 
-NCURSES_EXPORT(int)
-wins_nwstr(WINDOW *win, const wchar_t * wstr, int n)
+static int
+find_definition(struct tries *tree, const char *str)
 {
-    int code = ERR;
-    NCURSES_SIZE_T oy;
-    NCURSES_SIZE_T ox;
-    const wchar_t *cp;
+    struct tries *ptr;
+    int result = 0;
 
-    T((T_CALLED("wins_nwstr(%p,%s,%d)"), win, _nc_viswbufn(wstr,n), n));
-
-    if (win != 0
-       && wstr != 0
-       && wcwidth(*wstr) > 0) {
-       code = OK;
-       if (n < 1)
-           n = wcslen(wstr);
-       oy = win->_cury;
-       ox = win->_curx;
-       for (cp = wstr; *cp && ((cp - wstr) < n); cp++) {
-           NCURSES_CH_T wch;
-           SetChar2(wch, *cp);
-           if (*cp == '\n' || *cp == '\r' || *cp == '\t' || *cp == '\b') {
-               _nc_waddch_nosync(win, wch);
-           } else if (is7bits(*cp) && iscntrl(*cp)) {
-               winsch(win, ' ' + (chtype) (*cp));
-               winsch(win, (chtype) '^');
-               win->_curx += 2;
-           } else if (wins_wch(win, &wch) == ERR
-                      || win->_curx > win->_maxx) {
-               break;
+    if (str != 0 && *str != '\0') {
+       for (ptr = tree; ptr != 0; ptr = ptr->sibling) {
+           if (UChar(*str) == UChar(ptr->ch)) {
+               if (str[1] == '\0' && ptr->child != 0) {
+                   result = -1;
+               } else if ((result = find_definition(ptr->child, str + 1)) == 0) {
+                   result = ptr->value;
+               } else if (str[1] == '\0') {
+                   result = -1;
+               }
            }
+           if (result != 0)
+               break;
        }
+    }
+    return (result);
+}
 
-       win->_curx = ox;
-       win->_cury = oy;
-       _nc_synchook(win);
-       code = OK;
+/*
+ * Returns the keycode associated with the given string.  If none is found,
+ * return 0.  If the string is only a prefix to other strings, return -1.
+ */
+NCURSES_EXPORT(int)
+key_defined(const char *str)
+{
+    int code = ERR;
+
+    T((T_CALLED("key_defined(%s)"), _nc_visbuf(str)));
+    if (SP != 0 && str != 0) {
+       code = find_definition(SP->_keytry, str);
     }
+
     returnCode(code);
 }