OSDN Git Service

Use return value of write function
[hengband/hengband.git] / src / util.c
index f167b92..0da8e97 100644 (file)
@@ -215,7 +215,7 @@ errr path_parse(char *buf, int max, cptr file)
        u = file+1;
 
        /* Look for non-user portion of the file */
-       s = strstr(u, PATH_SEP);
+       s = my_strstr(u, PATH_SEP);
 
        /* Hack -- no long user names */
        if (s && (s >= u + sizeof(user))) return (1);
@@ -293,7 +293,11 @@ static errr path_temp(char *buf, int max)
        if (!s) return (-1);
 
        /* Format to length */
+#ifndef WIN32
        (void)strnfmt(buf, max, "%s", s);
+#else
+       (void)strnfmt(buf, max, ".%s", s);
+#endif
 
        /* Success */
        return (0);
@@ -356,19 +360,19 @@ FILE *my_fopen(cptr file, cptr mode)
 {
        char buf[1024];
 
-#if defined(MACINTOSH) && defined(MAC_MPW)
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
        FILE *tempfff;
 #endif
 
        /* Hack -- Try to parse the path */
        if (path_parse(buf, 1024, file)) return (NULL);
 
-#if defined(MACINTOSH) && defined(MAC_MPW)
-       if (strchr(mode, 'w'))
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
+       if (my_strchr(mode, 'w'))
        {
                /* setting file type/creator */
                tempfff = fopen(buf, mode);
-               fsetfileinfo(file, _fcreator, _ftype);
+               fsetfileinfo(buf, _fcreator, _ftype);
                fclose(tempfff);
        }
 #endif
@@ -482,7 +486,7 @@ errr my_fgets(FILE *fff, char *buf, huge n)
                                buf[i++] = ' ';
 
                                /* Append some more spaces */
-                               while (!(i % 8)) buf[i++] = ' ';
+                               while (0 != (i % 8)) buf[i++] = ' ';
                        }
 
 #ifdef JP
@@ -492,14 +496,13 @@ errr my_fgets(FILE *fff, char *buf, huge n)
                                buf[i++] = *s++;
                                buf[i++] = *s;
                        }
-# ifndef EUC
-       /* È¾³Ñ¤«¤Ê¤ËÂбþ */
-                       else if ((((int)*s & 0xff) > 0xa1) && (((int)*s & 0xff ) < 0xdf))
+
+                       /* È¾³Ñ¤«¤Ê¤ËÂбþ */
+                       else if (iskana(*s))
                        {
                                buf[i++] = *s;
                                if (i >= n) break;
                        }
-# endif
 #endif
                        /* Handle printables */
                        else if (isprint(*s))
@@ -658,7 +661,19 @@ errr fd_copy(cptr file, cptr what)
        /* Copy */
        while ((read_num = read(src_fd, buf, 1024)) > 0)
        {
-               write(dst_fd, buf, read_num);
+               int write_num = 0;
+               while (write_num < read_num)
+               {
+                       int ret = write(dst_fd, buf + write_num, read_num - write_num);
+                       if (ret < 0) {
+                               /* Close files */
+                               fd_close(src_fd);
+                               fd_close(dst_fd);
+
+                               return ret;
+                       }
+                       write_num += ret;
+               }
        }
 
        /* Close files */
@@ -703,16 +718,16 @@ int fd_make(cptr file, int mode)
 
 #else /* BEN_HACK */
 
-# if defined(MACINTOSH) && defined(MAC_MPW)
-
-       /* setting file type and creator -- AR */
+#if defined(MAC_MPW) || defined(MACH_O_CARBON)
        {
-               errr errr_tmp;
-               errr_tmp = open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
-               fsetfileinfo(file, _fcreator, _ftype);
-               return(errr_tmp);
+               int fdes;
+               /* Create the file, fail if exists, write-only, binary */
+               fdes = open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
+               /* Set creator and type if the file is successfully opened */
+               if (fdes >= 0) fsetfileinfo(buf, _fcreator, _ftype);
+               /* Return the descriptor */
+               return (fdes);
        }
-
 # else
        /* Create the file, fail if exists, write-only, binary */
        return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
@@ -1107,7 +1122,7 @@ static void trigger_text_to_ascii(char **bufptr, cptr *strptr)
        /* Invalid trigger name? */
        if (i == max_macrotrigger)
        {
-               str = strchr(str, ']');
+               str = my_strchr(str, ']');
                if (str)
                {
                        *s++ = (char)31;
@@ -1309,7 +1324,7 @@ static bool trigger_ascii_to_text(char **bufptr, cptr *strptr)
                switch(ch)
                {
                case '&':
-                       while ((tmp = strchr(macro_modifier_chr, *str)))
+                       while ((tmp = my_strchr(macro_modifier_chr, *str)))
                        {
                                j = (int)(tmp - macro_modifier_chr);
                                tmp = macro_modifier_name[j];
@@ -1789,10 +1804,10 @@ static char inkey_aux(void)
                else
                {
                        /* Increase "wait" */
-                       w += 10;
+                       w += 1;
 
                        /* Excessive delay */
-                       if (w >= 100) break;
+                       if (w >= 10) break;
 
                        /* Delay */
                        Term_xtra(TERM_XTRA_DELAY, w);
@@ -1862,6 +1877,33 @@ static char inkey_aux(void)
 
 
 /*
+ * Cancel macro action on the queue
+ */
+static void forget_macro_action(void)
+{
+       if (!parse_macro) return;
+
+       /* Drop following macro action string */
+       while (TRUE)
+       {
+               char ch;
+
+               /* End loop if no key ready */
+               if (Term_inkey(&ch, FALSE, TRUE)) break;
+
+               /* End loop if no key ready */
+               if (ch == 0) break;
+
+               /* End of "macro action" */
+               if (ch == 30) break;
+       }
+
+       /* No longer inside "macro action" */
+       parse_macro = FALSE;
+}
+
+
+/*
  * Mega-Hack -- special "inkey_next" pointer.  XXX XXX XXX
  *
  * This special pointer allows a sequence of keys to be "inserted" into
@@ -2190,6 +2232,22 @@ char inkey(void)
  */
 
 /*
+ * Initialize the quark array
+ */
+void quark_init(void)
+{
+       /* Quark variables */
+       C_MAKE(quark__str, QUARK_MAX, cptr);
+
+       /* Prepare first quark, which is used when quark_add() is failed */
+       quark__str[1] = string_make("");
+
+       /* There is one quark (+ NULL) */
+       quark__num = 2;
+}
+
+
+/*
  * Add a new "quark" to the set of quarks.
  */
 s16b quark_add(cptr str)
@@ -2203,8 +2261,8 @@ s16b quark_add(cptr str)
                if (streq(quark__str[i], str)) return (i);
        }
 
-       /* Paranoia -- Require room */
-       if (quark__num == QUARK_MAX) return (0);
+       /* Return "" when no room is available */
+       if (quark__num == QUARK_MAX) return 1;
 
        /* New maximal quark */
        quark__num = i + 1;
@@ -2224,8 +2282,8 @@ cptr quark_str(s16b i)
 {
        cptr q;
 
-       /* Verify */
-       if ((i < 0) || (i >= quark__num)) i = 0;
+       /* Return NULL for an invalid index */
+       if ((i < 1) || (i >= quark__num)) return NULL;
 
        /* Access the quark */
        q = quark__str[i];
@@ -2409,7 +2467,7 @@ void message_add(cptr str)
                }
 
                /* Limit the multiplier to 1000 */
-               if (buf && streq(buf, str) && (j < 1000))
+               if (streq(buf, str) && (j < 1000))
                {
                        j++;
 
@@ -3138,20 +3196,32 @@ void clear_from(int row)
 
 
 /*
- * Get some input at the cursor location.
+ * Get some string input at the cursor location.
  * Assume the buffer is initialized to a default string.
- * Note that this string is often "empty" (see below).
- * The default buffer is displayed in yellow until cleared.
- * Pressing RETURN right away accepts the default entry.
- * Normal chars clear the default and append the char.
- * Backspace clears the default or deletes the final char.
+ *
+ * The default buffer is in Overwrite mode and displayed in yellow at
+ * first.  Normal chars clear the yellow text and append the char in
+ * white text.
+ *
+ * LEFT (^B) and RIGHT (^F) movement keys move the cursor position.
+ * If the text is still displayed in yellow (Overwite mode), it will
+ * turns into white (Insert mode) when cursor moves.
+ *
+ * DELETE (^D) deletes a char at the cursor position.
+ * BACKSPACE (^H) deletes a char at the left of cursor position.
  * ESCAPE clears the buffer and the window and returns FALSE.
  * RETURN accepts the current buffer contents and returns TRUE.
  */
-bool askfor_aux(char *buf, int len)
+bool askfor_aux(char *buf, int len, bool numpad_cursor)
 {
        int y, x;
        int pos = 0;
+
+       /*
+        * Text color
+        * TERM_YELLOW : Overwrite mode
+        * TERM_WHITE : Insert mode
+        */
        byte color = TERM_YELLOW;
 
        /* Locate the cursor position */
@@ -3183,7 +3253,7 @@ bool askfor_aux(char *buf, int len)
                Term_gotoxy(x + pos, y);
 
                /* Get a special key code */
-               skey = inkey_special();
+               skey = inkey_special(numpad_cursor);
 
                /* Analyze the key */
                switch (skey)
@@ -3193,7 +3263,10 @@ bool askfor_aux(char *buf, int len)
                {
                        int i = 0;
 
-                       /* No effect at biggining of line */
+                       /* Now on insert mode */
+                       color = TERM_WHITE;
+
+                       /* No move at beginning of line */
                        if (0 == pos) break;
 
                        while (TRUE)
@@ -3201,7 +3274,7 @@ bool askfor_aux(char *buf, int len)
                                int next_pos = i + 1;
 
 #ifdef JP
-                               if (iskanji(buf[next_pos])) next_pos++;
+                               if (iskanji(buf[i])) next_pos++;
 #endif
 
                                /* Is there the cursor at next position? */ 
@@ -3214,15 +3287,15 @@ bool askfor_aux(char *buf, int len)
                        /* Get previous position */
                        pos = i;
 
-                       /* Now on insert mode */
-                       color = TERM_WHITE;
-
                        break;
                }
 
                case SKEY_RIGHT:
                case KTRL('f'):
-                       /* No effect at end of line */
+                       /* Now on insert mode */
+                       color = TERM_WHITE;
+
+                       /* No move at end of line */
                        if ('\0' == buf[pos]) break;
 
 #ifdef JP
@@ -3233,9 +3306,6 @@ bool askfor_aux(char *buf, int len)
                        pos++;
 #endif
 
-                       /* Now on insert mode */
-                       color = TERM_WHITE;
-
                        break;
 
                case ESCAPE:
@@ -3250,30 +3320,58 @@ bool askfor_aux(char *buf, int len)
 
                case '\010':
                        /* Backspace */
+               {
+                       int i = 0;
+
+                       /* Now on insert mode */
+                       color = TERM_WHITE;
+
+                       /* No move at beginning of line */
+                       if (0 == pos) break;
+
+                       while (TRUE)
+                       {
+                               int next_pos = i + 1;
+
+#ifdef JP
+                               if (iskanji(buf[i])) next_pos++;
+#endif
+
+                               /* Is there the cursor at next position? */ 
+                               if (next_pos >= pos) break;
 
-                       /* No effect at biggining of line */
-                       if (!pos) break;
+                               /* Move to next */
+                               i = next_pos;
+                       }
 
-                       /* Go left 1 unit */
-                       pos--;
+                       /* Get previous position */
+                       pos = i;
 
                        /* Fall through to 'Delete key' */
+               }
 
                case 0x7F:
                case KTRL('d'):
                        /* Delete key */
                {
+                       int dst, src;
+
+                       /* Now on insert mode */
+                       color = TERM_WHITE;
 
-                       int dst = pos;
+                       /* No move at end of line */
+                       if ('\0' == buf[pos]) break;
 
                        /* Position of next character */
-                       int src = pos + 1;
+                       src = pos + 1;
 
 #ifdef JP
                        /* Next character is one more byte away */
-                       if (iskanji(src)) src++;
+                       if (iskanji(buf[pos])) src++;
 #endif
 
+                       dst = pos;
+
                        /* Move characters at src to dst */
                        while ('\0' != (buf[dst++] = buf[src++]))
                                /* loop */;
@@ -3357,6 +3455,17 @@ bool askfor_aux(char *buf, int len)
 
 
 /*
+ * Get some string input at the cursor location.
+ *
+ * Allow to use numpad keys as cursor keys.
+ */
+bool askfor(char *buf, int len)
+{
+       return askfor_aux(buf, len, TRUE);
+}
+
+
+/*
  * Get a string from the user
  *
  * The "prompt" should take the form "Prompt: "
@@ -3377,7 +3486,7 @@ bool get_string(cptr prompt, char *buf, int len)
        prt(prompt, 0, 0);
 
        /* Ask the user for a string */
-       res = askfor_aux(buf, len);
+       res = askfor(buf, len);
 
        /* Clear prompt */
        prt("", 0, 0);
@@ -3529,7 +3638,10 @@ bool get_com(cptr prompt, char *command, bool z_escape)
        prt(prompt, 0, 0);
 
        /* Get a key */
-       *command = inkey();
+       if (get_com_no_macros)
+               *command = inkey_special(FALSE);
+       else
+               *command = inkey();
 
        /* Clear the prompt */
        prt("", 0, 0);
@@ -3550,10 +3662,9 @@ bool get_com(cptr prompt, char *command, bool z_escape)
  */
 s16b get_quantity(cptr prompt, int max)
 {
+       bool res;
        int amt;
-
        char tmp[80];
-
        char buf[80];
 
 
@@ -3595,7 +3706,7 @@ s16b get_quantity(cptr prompt, int max)
        {
                /* Build a prompt */
 #ifdef JP
-                       sprintf(tmp, "¤¤¤¯¤Ä¤Ç¤¹¤« (1-%d): ", max);
+               sprintf(tmp, "¤¤¤¯¤Ä¤Ç¤¹¤« (1-%d): ", max);
 #else
                sprintf(tmp, "Quantity (1-%d): ", max);
 #endif
@@ -3605,6 +3716,11 @@ s16b get_quantity(cptr prompt, int max)
                prompt = tmp;
        }
 
+       /* Paranoia XXX XXX XXX */
+       msg_print(NULL);
+
+       /* Display prompt */
+       prt(prompt, 0, 0);
 
        /* Default to one */
        amt = 1;
@@ -3612,8 +3728,17 @@ s16b get_quantity(cptr prompt, int max)
        /* Build the default */
        sprintf(buf, "%d", amt);
 
-       /* Ask for a quantity */
-       if (!get_string(prompt, buf, 6)) return (0);
+       /*
+        * Ask for a quantity
+        * Don't allow to use numpad as cursor key.
+        */
+       res = askfor_aux(buf, 6, FALSE);
+
+       /* Clear prompt */
+       prt("", 0, 0);
+
+       /* Cancelled */
+       if (!res) return 0;
 
        /* Extract a number */
        amt = atoi(buf);
@@ -3812,7 +3937,7 @@ menu_naiyou menu_info[10][10] =
                {"Items(other)", 4, FALSE},
                {"Equip", 5, FALSE},
                {"Door/Box", 6, FALSE},
-               {"Infomations", 7, FALSE},
+               {"Informations", 7, FALSE},
                {"Options", 8, FALSE},
                {"Other commands", 9, FALSE},
                {"", 0, FALSE},
@@ -3839,7 +3964,7 @@ menu_naiyou menu_info[10][10] =
                {"Target(*)", '*', TRUE},
                {"Dig(T/^t)", 'T', TRUE},
                {"Go up stairs(<)", '<', TRUE},
-               {"Go down staies(>)", '>', TRUE},
+               {"Go down stairs(>)", '>', TRUE},
                {"Command pets(p)", 'p', TRUE},
                {"Search mode ON/OFF(S/#)", 'S', TRUE}
        },
@@ -3904,7 +4029,7 @@ menu_naiyou menu_info[10][10] =
                {"Identify symbol(/)", '/', TRUE},
                {"Show prev messages(^p)", KTRL('P'), TRUE},
                {"Current time(^t/')", KTRL('T'), TRUE},
-               {"Various infomations(~)", '~', TRUE},
+               {"Various informations(~)", '~', TRUE},
                {"Play record menu(|)", '|', TRUE},
                {"", 0, FALSE}
        },
@@ -3954,9 +4079,13 @@ special_menu_naiyou special_menu_info[] =
 {
        {"ĶǽÎÏ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_MINDCRAFTER},
        {"¤â¤Î¤Þ¤Í/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_IMITATOR},
+       {"²Î/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_BARD},
        {"ɬ»¦µ»/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_SAMURAI},
        {"Îýµ¤½Ñ/ËâË¡/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_FORCETRAINER},
+       {"µ»/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_BERSERKER},
+       {"µ»½Ñ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_SMITH},
        {"¶ÀËâË¡/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_MIRROR_MASTER},
+       {"Ǧ½Ñ/ÆüìǽÎÏ", 0, 0, MENU_CLASS, CLASS_NINJA},
        {"¹­°è¥Þ¥Ã¥×(<)", 2, 6, MENU_WILD, FALSE},
        {"Ä̾ï¥Þ¥Ã¥×(>)", 2, 7, MENU_WILD, TRUE},
        {"", 0, 0, 0, 0},
@@ -3966,9 +4095,13 @@ special_menu_naiyou special_menu_info[] =
 {
        {"MindCraft/Special", 0, 0, MENU_CLASS, CLASS_MINDCRAFTER},
        {"Imitation/Special", 0, 0, MENU_CLASS, CLASS_IMITATOR},
+       {"Song/Special", 0, 0, MENU_CLASS, CLASS_BARD},
        {"Technique/Special", 0, 0, MENU_CLASS, CLASS_SAMURAI},
        {"Mind/Magic/Special", 0, 0, MENU_CLASS, CLASS_FORCETRAINER},
+       {"BrutalPower/Special", 0, 0, MENU_CLASS, CLASS_BERSERKER},
+       {"Technique/Special", 0, 0, MENU_CLASS, CLASS_SMITH},
        {"MirrorMagic/Special", 0, 0, MENU_CLASS, CLASS_MIRROR_MASTER},
+       {"Ninjutsu/Special", 0, 0, MENU_CLASS, CLASS_NINJA},
        {"Enter global map(<)", 2, 6, MENU_WILD, FALSE},
        {"Enter local map(>)", 2, 7, MENU_WILD, TRUE},
        {"", 0, 0, 0, 0},
@@ -4398,7 +4531,7 @@ prt(format("
        if (always_repeat && (command_arg <= 0))
        {
                /* Hack -- auto repeat certain commands */
-               if (strchr("TBDoc+", command_cmd))
+               if (my_strchr("TBDoc+", command_cmd))
                {
                        /* Repeat 99 times */
                        command_arg = 99;
@@ -4439,11 +4572,6 @@ prt(format("
                caretcmd = command_cmd;
 #endif
 
-#ifdef JP
-#undef strchr
-#define strchr strchr_j
-#endif
-
        /* Hack -- Scan equipment */
        for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
        {
@@ -4461,7 +4589,7 @@ prt(format("
                s = quark_str(o_ptr->inscription);
 
                /* Find a '^' */
-               s = strchr(s, '^');
+               s = my_strchr(s, '^');
 
                /* Process preventions */
                while (s)
@@ -4488,7 +4616,7 @@ prt(format("
                        }
 
                        /* Find another '^' */
-                       s = strchr(s + 1, '^');
+                       s = my_strchr(s + 1, '^');
                }
        }
 
@@ -4540,7 +4668,7 @@ static bool insert_str(char *buf, cptr target, cptr insert)
        int                b_len, t_len, i_len;
 
        /* Attempt to find the target (modify "buf") */
-       buf = strstr(buf, target);
+       buf = my_strstr(buf, target);
 
        /* No target found */
        if (!buf) return (FALSE);
@@ -4737,14 +4865,9 @@ static void swap(tag_type *a, tag_type *b)
 {
        tag_type temp;
 
-       temp.tag = a->tag;
-       temp.pointer = a->pointer;
-
-       a->tag = b->tag;
-       a->pointer = b->pointer;
-
-       b->tag = temp.tag;
-       b->pointer = temp.pointer;
+       temp = *a;
+       *a = *b;
+       *b = temp;
 }
 
 
@@ -5105,26 +5228,29 @@ size_t my_strcpy(char *buf, const char *src, size_t bufsize)
        const char *s = src;
        size_t len = 0;
 
-       /* reserve for NUL termination */
-       bufsize--;
+       if (bufsize > 0) {
+               /* reserve for NUL termination */
+               bufsize--;
 
-       /* Copy as many bytes as will fit */
-       while (len < bufsize)
-       {
-               if (iskanji(*s))
-               {
-                       if (len + 1 >= bufsize || !*(s+1)) break;
-                       *d++ = *s++;
-                       *d++ = *s++;
-                       len += 2;
-               }
-               else
+               /* Copy as many bytes as will fit */
+               while (*s && (len < bufsize))
                {
-                       *d++ = *s++;
-                       len++;
+                       if (iskanji(*s))
+                       {
+                               if (len + 1 >= bufsize || !*(s+1)) break;
+                               *d++ = *s++;
+                               *d++ = *s++;
+                               len += 2;
+                       }
+                       else
+                       {
+                               *d++ = *s++;
+                               len++;
+                       }
                }
+               *d = '\0';
        }
-       *d = '\0';
+
        while(*s++) len++;
 
        return len;
@@ -5181,13 +5307,81 @@ size_t my_strcat(char *buf, const char *src, size_t bufsize)
 
 
 /*
+ * A copy of ANSI strstr()
+ *
+ * my_strstr() can handle Kanji strings correctly.
+ */
+char *my_strstr(const char *haystack, const char *needle)
+{
+       int i;
+       int l1 = strlen(haystack);
+       int l2 = strlen(needle);
+
+       if (l1 >= l2)
+       {
+               for(i = 0; i <= l1 - l2; i++)
+               {
+                       if(!strncmp(haystack + i, needle, l2))
+                               return (char *)haystack + i;
+
+#ifdef JP
+                       if (iskanji(*(haystack + i))) i++;
+#endif
+               }
+       }
+
+       return NULL;
+}
+
+
+/*
+ * A copy of ANSI strchr()
+ *
+ * my_strchr() can handle Kanji strings correctly.
+ */
+char *my_strchr(const char *ptr, char ch)
+{
+       for ( ; *ptr != '\0'; ptr++)
+       {
+               if (*ptr == ch) return (char *)ptr;
+
+#ifdef JP
+               if (iskanji(*ptr)) ptr++;
+#endif
+       }
+
+       return NULL;
+}
+
+
+/*
+ * Convert string to lower case
+ */
+void str_tolower(char *str)
+{
+       /* Force to be lower case string */
+       for (; *str; str++)
+       {
+#ifdef JP
+               if (iskanji(*str))
+               {
+                       str++;
+                       continue;
+               }
+#endif
+               *str = tolower(*str);
+       }
+}
+
+
+/*
  * Get a keypress from the user.
  * And interpret special keys as internal code.
  *
  * This function is a Mega-Hack and depend on pref-xxx.prf's.
  * Currently works on Linux(UNIX), Windows, and Macintosh only.
  */
-int inkey_special(void)
+int inkey_special(bool numpad_cursor)
 {
        static const struct {
                cptr keyname;
@@ -5199,34 +5393,35 @@ int inkey_special(void)
        };
 
        static const struct {
+               bool numpad;
                cptr keyname;
                int keycode;
        } special_key_list[] = {
-               {"Down]", SKEY_DOWN},
-               {"Left]", SKEY_LEFT},
-               {"Right]", SKEY_RIGHT},
-               {"Up]", SKEY_UP},
-               {"Page_Up]", SKEY_PGUP},
-               {"Page_Down]", SKEY_PGDOWN},
-               {"Home]", SKEY_TOP},
-               {"End]", SKEY_BOTTOM},
-               {"KP_Down]", SKEY_DOWN},
-               {"KP_Left]", SKEY_LEFT},
-               {"KP_Right]", SKEY_RIGHT},
-               {"KP_Up]", SKEY_UP},
-               {"KP_Page_Up]", SKEY_PGUP},
-               {"KP_Page_Down]", SKEY_PGDOWN},
-               {"KP_Home]", SKEY_TOP},
-               {"KP_End]", SKEY_BOTTOM},
-               {"KP_2]", SKEY_DOWN},
-               {"KP_4]", SKEY_LEFT},
-               {"KP_6]", SKEY_RIGHT},
-               {"KP_8]", SKEY_UP},
-               {"KP_9]", SKEY_PGUP},
-               {"KP_3]", SKEY_PGDOWN},
-               {"KP_7]", SKEY_TOP},
-               {"KP_1]", SKEY_BOTTOM},
-               {NULL, 0},
+               {FALSE, "Down]", SKEY_DOWN},
+               {FALSE, "Left]", SKEY_LEFT},
+               {FALSE, "Right]", SKEY_RIGHT},
+               {FALSE, "Up]", SKEY_UP},
+               {FALSE, "Page_Up]", SKEY_PGUP},
+               {FALSE, "Page_Down]", SKEY_PGDOWN},
+               {FALSE, "Home]", SKEY_TOP},
+               {FALSE, "End]", SKEY_BOTTOM},
+               {TRUE, "KP_Down]", SKEY_DOWN},
+               {TRUE, "KP_Left]", SKEY_LEFT},
+               {TRUE, "KP_Right]", SKEY_RIGHT},
+               {TRUE, "KP_Up]", SKEY_UP},
+               {TRUE, "KP_Page_Up]", SKEY_PGUP},
+               {TRUE, "KP_Page_Down]", SKEY_PGDOWN},
+               {TRUE, "KP_Home]", SKEY_TOP},
+               {TRUE, "KP_End]", SKEY_BOTTOM},
+               {TRUE, "KP_2]", SKEY_DOWN},
+               {TRUE, "KP_4]", SKEY_LEFT},
+               {TRUE, "KP_6]", SKEY_RIGHT},
+               {TRUE, "KP_8]", SKEY_UP},
+               {TRUE, "KP_9]", SKEY_PGUP},
+               {TRUE, "KP_3]", SKEY_PGDOWN},
+               {TRUE, "KP_7]", SKEY_TOP},
+               {TRUE, "KP_1]", SKEY_BOTTOM},
+               {FALSE, NULL, 0},
        };
        char buf[1024];
        cptr str = buf;
@@ -5236,88 +5431,91 @@ int inkey_special(void)
        int i;
        size_t trig_len;
 
+       /*
+        * Forget macro trigger ----
+        * It's important if we are already expanding macro action
+        */
+       inkey_macro_trigger_string[0] = '\0';
+
        /* Get a keypress */
        key = inkey();
 
        /* Examine trigger string */
        trig_len = strlen(inkey_macro_trigger_string);
 
-       /* No special key */
-       if (!trig_len) return (int)key;
+       /* Already known that no special key */
+       if (!trig_len) return (int)((unsigned char)key);
 
        /*
-        * Mega Hack -- ignore macro defined on ASCII keys
-        *
-        * When this function is used, all ASCII keys are used as
-        * themselfs instead of macro triggers for command macro's.
+        * Hack -- Ignore macro defined on ASCII characters.
         */
-#ifdef JP
-       if (trig_len == 1 && !iskanji(inkey_macro_trigger_string[0]))
-#else
-       if (trig_len == 1)
-#endif
+       if (trig_len == 1 && parse_macro)
        {
-               /* Get original key */
-               key = inkey_macro_trigger_string[0];
-               
-               /* Kill further macro expansion */
-               flush();
+               char c = inkey_macro_trigger_string[0];
+
+               /* Cancel macro action on the queue */
+               forget_macro_action();
 
                /* Return the originaly pressed key */
-               return (int)key;
+               return (int)((unsigned char)c);
        }
 
        /* Convert the trigger */
        ascii_to_text(buf, inkey_macro_trigger_string);
 
        /* Check the prefix "\[" */
-       if (!prefix(str, "\\[")) return 0;
-
-       /* Skip "\[" */
-       str += 2;
-
-       /* Examine modifier keys */
-       while (TRUE)
+       if (prefix(str, "\\["))
        {
-               for (i = 0; modifier_key_list[i].keyname; i++)
+               /* Skip "\[" */
+               str += 2;
+
+               /* Examine modifier keys */
+               while (TRUE)
                {
-                       if (prefix(str, modifier_key_list[i].keyname))
+                       for (i = 0; modifier_key_list[i].keyname; i++)
                        {
-                               /* Get modifier key flag */
-                               str += strlen(modifier_key_list[i].keyname);
-                               modifier |= modifier_key_list[i].keyflag;
+                               if (prefix(str, modifier_key_list[i].keyname))
+                               {
+                                       /* Get modifier key flag */
+                                       str += strlen(modifier_key_list[i].keyname);
+                                       modifier |= modifier_key_list[i].keyflag;
+                               }
                        }
+
+                       /* No more modifier key found */
+                       if (!modifier_key_list[i].keyname) break;
                }
 
-               /* No more modifier key found */
-               if (!modifier_key_list[i].keyname) break;
-       }
+               /* numpad_as_cursorkey option force numpad keys to input numbers */
+               if (!numpad_as_cursorkey) numpad_cursor = FALSE;
 
-       /* Get a special key code */
-       for (i = 0; special_key_list[i].keyname; i++)
-       {
-               if (streq(str, special_key_list[i].keyname))
+               /* Get a special key code */
+               for (i = 0; special_key_list[i].keyname; i++)
                {
-                       skey = special_key_list[i].keycode;
-                       break;
+                       if ((!special_key_list[i].numpad || numpad_cursor) &&
+                           streq(str, special_key_list[i].keyname))
+                       {
+                               skey = special_key_list[i].keycode;
+                               break;
+                       }
                }
-       }
 
-       /* No special key found? */
-       if (!skey)
-       {
-               /* Don't bother with this trigger no more */
-               inkey_macro_trigger_string[0] = '\0';
+               /* A special key found */
+               if (skey)
+               {
+                       /* Cancel macro action on the queue */
+                       forget_macro_action();
 
-               /* Return normal keycode */
-               return (int)key;
+                       /* Return special key code and modifier flags */
+                       return (skey | modifier);
+               }
        }
 
-       /* A special key found */
+       /* No special key found? */
 
-       /* Kill further macro expansion */
-       flush();
+       /* Don't bother with this trigger no more */
+       inkey_macro_trigger_string[0] = '\0';
 
-       /* Return special key code and modifier flags */
-       return (skey | modifier);
+       /* Return normal keycode */
+       return (int)((unsigned char)key);
 }