OSDN Git Service

inkey_special()でテンキーは特別扱いしない(数値入力で固定する)ようにした。
[hengband/hengband.git] / src / autopick.c
1 /* File: autopick.c */
2
3 /* Purpose: Object Auto-picker/Destroyer */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15
16 #define MAX_LINELEN 1024
17
18 /*
19  * Macros for Keywords
20  */
21 #define FLG_ALL             0
22 #define FLG_COLLECTING      1
23 #define FLG_UNAWARE         2 
24 #define FLG_UNIDENTIFIED    3 
25 #define FLG_IDENTIFIED      4 
26 #define FLG_STAR_IDENTIFIED 5 
27 #define FLG_BOOSTED         6 
28 #define FLG_MORE_THAN       7 
29 #define FLG_DICE            8 
30 #define FLG_MORE_BONUS      9 
31 #define FLG_MORE_BONUS2     10
32 #define FLG_WORTHLESS       11
33 #define FLG_ARTIFACT        12
34 #define FLG_EGO             13
35 #define FLG_NAMELESS        14
36 #define FLG_WANTED          15
37 #define FLG_UNIQUE          16
38 #define FLG_HUMAN           17
39 #define FLG_UNREADABLE      18
40 #define FLG_REALM1          19
41 #define FLG_REALM2          20
42 #define FLG_FIRST           21
43 #define FLG_SECOND          22
44 #define FLG_THIRD           23
45 #define FLG_FOURTH          24
46
47 #define FLG_ITEMS           30
48 #define FLG_WEAPONS         31
49 #define FLG_ARMORS          32
50 #define FLG_MISSILES        33
51 #define FLG_DEVICES         34
52 #define FLG_LIGHTS          35
53 #define FLG_JUNKS           36
54 #define FLG_SPELLBOOKS      37
55 #define FLG_HAFTED          38
56 #define FLG_SHIELDS         39
57 #define FLG_BOWS            40
58 #define FLG_RINGS           41
59 #define FLG_AMULETS         42
60 #define FLG_SUITS           43
61 #define FLG_CLOAKS          44
62 #define FLG_HELMS           45
63 #define FLG_GLOVES          46
64 #define FLG_BOOTS           47
65 #define FLG_FAVORITE        48
66
67 #define FLG_NOUN_BEGIN      FLG_ITEMS
68 #define FLG_NOUN_END        FLG_FAVORITE
69
70 #ifdef JP
71
72 #define KEY_ALL "¤¹¤Ù¤Æ¤Î"
73
74 #ifdef MAC_MPW
75 /*
76  * MEGA HACK -- MPW¤Î¥Ð¥°½ü¤±¡£
77  * pre-processÃæ¤Ë¡Ö¼ý¡×¤Î»ú¤Î2¥Ð¥¤¥ÈÌܤ¬¾¡¼ê¤Ë¾Ã¤¨¤Æ¤·¤Þ¤¦¡£
78  */
79 #define KEY_COLLECTING "\x8e\xfb½¸Ãæ¤Î"
80 #else
81 #define KEY_COLLECTING "¼ý½¸Ãæ¤Î"
82 #endif
83
84 #define KEY_UNIDENTIFIED "̤´ÕÄê¤Î"
85 #define KEY_IDENTIFIED "´ÕÄêºÑ¤ß¤Î"
86 #define KEY_STAR_IDENTIFIED "*´ÕÄê*ºÑ¤ß¤Î"
87 #define KEY_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦"
88 #define KEY_MORE_THAN  "¥À¥¤¥¹ÌÜ"
89 #define KEY_DICE  "°Ê¾å¤Î"
90 #define KEY_MORE_BONUS  "½¤ÀµÃÍ"
91 #define KEY_MORE_BONUS2  "°Ê¾å¤Î"
92 #define KEY_WORTHLESS "̵²ÁÃͤÎ"
93 #define KEY_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È"
94 #define KEY_EGO "¥¨¥´"
95 #define KEY_NAMELESS "̵ÌäÎ"
96 #define KEY_UNAWARE "̤ȽÌÀ¤Î"
97 #define KEY_WANTED "¾Þ¶â¼ó¤Î"
98 #define KEY_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î"
99 #define KEY_HUMAN "¿Í´Ö¤Î"
100 #define KEY_UNREADABLE "Æɤá¤Ê¤¤"
101 #define KEY_REALM1 "Âè°ìÎΰè¤Î"
102 #define KEY_REALM2 "ÂèÆóÎΰè¤Î"
103 #define KEY_FIRST "1ºýÌܤÎ"
104 #define KEY_SECOND "2ºýÌܤÎ"
105 #define KEY_THIRD "3ºýÌܤÎ"
106 #define KEY_FOURTH "4ºýÌܤÎ"
107 #define KEY_ITEMS "¥¢¥¤¥Æ¥à"
108 #define KEY_WEAPONS "Éð´ï"
109 #define KEY_ARMORS "Ëɶñ"
110 #define KEY_MISSILES "Ìð"
111 #define KEY_DEVICES "ËâË¡¥¢¥¤¥Æ¥à"
112 #define KEY_LIGHTS "¸÷¸»"
113 #define KEY_JUNKS "¤¬¤é¤¯¤¿"
114 #define KEY_SPELLBOOKS "ËâË¡½ñ"
115 #define KEY_HAFTED "Æß´ï"
116 #define KEY_SHIELDS "½â"
117 #define KEY_BOWS "µÝ"
118 #define KEY_RINGS "»ØÎØ"
119 #define KEY_AMULETS "¥¢¥ß¥å¥ì¥Ã¥È"
120 #define KEY_SUITS "³»"
121 #define KEY_CLOAKS "¥¯¥í¡¼¥¯"
122 #define KEY_HELMS "³õ"
123 #define KEY_GLOVES "äƼê"
124 #define KEY_BOOTS "·¤"
125 #define KEY_FAVORITE "ÆÀ°ÕÉð´ï"
126
127 #else 
128
129 #define KEY_ALL "all"
130 #define KEY_COLLECTING "collecting"
131 #define KEY_UNIDENTIFIED "unidentified"
132 #define KEY_IDENTIFIED "identified"
133 #define KEY_STAR_IDENTIFIED "*identified*"
134 #define KEY_BOOSTED "dice boosted"
135 #define KEY_MORE_THAN  "more than"
136 #define KEY_DICE  " dice"
137 #define KEY_MORE_BONUS  "more bonus than"
138 #define KEY_MORE_BONUS2  ""
139 #define KEY_WORTHLESS "worthless"
140 #define KEY_ARTIFACT "artifact"
141 #define KEY_EGO "ego"
142 #define KEY_NAMELESS "nameless"
143 #define KEY_UNAWARE "unaware"
144 #define KEY_WANTED "wanted"
145 #define KEY_UNIQUE "unique monster's"
146 #define KEY_HUMAN "human"
147 #define KEY_UNREADABLE "unreadable"
148 #define KEY_REALM1 "first realm's"
149 #define KEY_REALM2 "second realm's"
150 #define KEY_FIRST "first"
151 #define KEY_SECOND "second"
152 #define KEY_THIRD "third"
153 #define KEY_FOURTH "fourth"
154 #define KEY_ITEMS "items"
155 #define KEY_WEAPONS "weapons"
156 #define KEY_ARMORS "armors"
157 #define KEY_MISSILES "missiles"
158 #define KEY_DEVICES "magical devices"
159 #define KEY_LIGHTS "lights"
160 #define KEY_JUNKS "junks"
161 #define KEY_SPELLBOOKS "spellbooks"
162 #define KEY_HAFTED "hafted weapons"
163 #define KEY_SHIELDS "shields"
164 #define KEY_BOWS "bows"
165 #define KEY_RINGS "rings"
166 #define KEY_AMULETS "amulets"
167 #define KEY_SUITS "suits"
168 #define KEY_CLOAKS "cloaks"
169 #define KEY_HELMS "helms"
170 #define KEY_GLOVES "gloves"
171 #define KEY_BOOTS "boots"
172 #define KEY_FAVORITE "favorite weapons"
173
174 #endif /* JP */
175
176 #define MATCH_KEY(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
177      ? (ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
178 #define MATCH_KEY2(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
179      ? (prev_ptr = ptr, ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
180
181 #ifdef JP
182 #define ADD_KEY(KEY) strcat(ptr, KEY)
183 #else
184 #define ADD_KEY(KEY) (strcat(ptr, KEY), strcat(ptr, " "))
185 #endif
186 #define ADD_KEY2(KEY) strcat(ptr, KEY)
187
188 #define ADD_FLG(FLG) (entry->flag[FLG / 32] |= (1L << (FLG % 32)))
189 #define REM_FLG(FLG) (entry->flag[FLG / 32] &= ~(1L << (FLG % 32)))
190 #define ADD_FLG_NOUN(FLG) (ADD_FLG(FLG), prev_flg = FLG)
191 #define IS_FLG(FLG) (entry->flag[FLG / 32] & (1L << (FLG % 32)))
192
193 #ifdef JP
194         static char kanji_colon[] = "¡§";
195 #endif
196
197
198 /*
199  * A function to create new entry
200  */
201 static bool autopick_new_entry(autopick_type *entry, cptr str, bool allow_default)
202 {
203         cptr insc;
204         int i;
205         byte act = 0;
206         char buf[MAX_LINELEN];
207         cptr prev_ptr, ptr, old_ptr;
208         int prev_flg;
209
210         if (str[1] == ':') switch (str[0])
211         {
212         case '?': case '%':
213         case 'A': case 'P': case 'C':
214                 return FALSE;
215         }
216
217         entry->flag[0] = entry->flag[1] = 0L;
218         entry->dice = 0;
219
220         act = DO_AUTOPICK | DO_DISPLAY;
221         while (1)
222         {
223                 if ((act & DO_AUTOPICK) && *str == '!')
224                 {
225                         act &= ~DO_AUTOPICK;
226                         act |= DO_AUTODESTROY;
227                         str++;
228                 }
229                 else if ((act & DO_AUTOPICK) && *str == '~')
230                 {
231                         act &= ~DO_AUTOPICK;
232                         act |= DONT_AUTOPICK;
233                         str++;
234                 }
235                 else if ((act & DO_AUTOPICK) && *str == ';')
236                 {
237                         act &= ~DO_AUTOPICK;
238                         act |= DO_QUERY_AUTOPICK;
239                         str++;
240                 }
241                 else if ((act & DO_DISPLAY) && *str == '(')
242                 {
243                         act &= ~DO_DISPLAY;
244                         str++;
245                 }
246                 else
247                         break;
248         }
249
250         /* don't mind upper or lower case */
251         insc = NULL;
252         for (i = 0; *str; i++)
253         {
254                 char c = *str++;
255 #ifdef JP
256                 if (iskanji(c))
257                 {
258                         buf[i++] = c;
259                         buf[i] = *str++;
260                         continue;
261                 }
262 #endif
263                 /* Auto-inscription? */
264                 if (c == '#')
265                 {
266                         buf[i] = '\0';
267                         insc = str;
268                         break;
269                 }
270
271                 if (isupper(c)) c = tolower(c);
272
273                 buf[i] = c;
274         }
275         buf[i] = '\0';
276
277         /* Skip empty line unless allow_default */
278         if (!allow_default && *buf == 0) return FALSE;
279
280         /* Skip comment line */
281         if (*buf == 0 && insc) return FALSE;
282
283         ptr = prev_ptr = buf;
284         old_ptr = NULL;
285
286         while (old_ptr != ptr)
287         {
288                 /* Save current location */
289                 old_ptr = ptr;
290
291                 if (MATCH_KEY(KEY_ALL)) ADD_FLG(FLG_ALL);
292                 if (MATCH_KEY(KEY_COLLECTING)) ADD_FLG(FLG_COLLECTING);
293                 if (MATCH_KEY(KEY_UNIDENTIFIED)) ADD_FLG(FLG_UNIDENTIFIED);
294                 if (MATCH_KEY(KEY_IDENTIFIED)) ADD_FLG(FLG_IDENTIFIED);
295                 if (MATCH_KEY(KEY_STAR_IDENTIFIED)) ADD_FLG(FLG_STAR_IDENTIFIED);
296                 if (MATCH_KEY(KEY_BOOSTED)) ADD_FLG(FLG_BOOSTED);
297
298                 /*** Weapons whose dd*ds is more than nn ***/
299                 if (MATCH_KEY2(KEY_MORE_THAN))
300                 {
301                         int k = 0;
302                         entry->dice = 0;
303
304                         /* Drop leading spaces */
305                         while (' ' == *ptr) ptr++;
306
307                         /* Read number */
308                         while ('0' <= *ptr && *ptr <= '9')
309                         {
310                                 entry->dice = 10 * entry->dice + (*ptr - '0');
311                                 ptr++;
312                                 k++;
313                         }
314
315                         if (k > 0 && k <= 2)
316                         {
317                                 (void)MATCH_KEY(KEY_DICE);
318                                 ADD_FLG(FLG_MORE_THAN);
319                         }
320                         else
321                                 ptr = prev_ptr;
322                 }
323
324                 /*** Items whose magical bonus is more than n ***/
325                 if (MATCH_KEY2(KEY_MORE_BONUS))
326                 {
327                         int k = 0;
328                         entry->bonus = 0;
329
330                         /* Drop leading spaces */
331                         while (' ' == *ptr) ptr++;
332
333                         /* Read number */
334                         while ('0' <= *ptr && *ptr <= '9')
335                         {
336                                 entry->bonus = 10 * entry->bonus + (*ptr - '0');
337                                 ptr++;
338                                 k++;
339                         }
340
341                         if (k > 0 && k <= 2)
342                         {
343                                 (void)MATCH_KEY(KEY_MORE_BONUS2);
344                                 ADD_FLG(FLG_MORE_BONUS);
345                         }
346                         else
347                                 ptr = prev_ptr;
348                 }
349
350                 if (MATCH_KEY(KEY_WORTHLESS)) ADD_FLG(FLG_WORTHLESS);
351                 if (MATCH_KEY(KEY_EGO)) ADD_FLG(FLG_EGO);
352                 if (MATCH_KEY(KEY_NAMELESS)) ADD_FLG(FLG_NAMELESS);
353                 if (MATCH_KEY(KEY_UNAWARE)) ADD_FLG(FLG_UNAWARE);
354                 if (MATCH_KEY(KEY_WANTED)) ADD_FLG(FLG_WANTED);
355                 if (MATCH_KEY(KEY_UNIQUE)) ADD_FLG(FLG_UNIQUE);
356                 if (MATCH_KEY(KEY_HUMAN)) ADD_FLG(FLG_HUMAN);
357                 if (MATCH_KEY(KEY_UNREADABLE)) ADD_FLG(FLG_UNREADABLE);
358                 if (MATCH_KEY(KEY_REALM1)) ADD_FLG(FLG_REALM1);
359                 if (MATCH_KEY(KEY_REALM2)) ADD_FLG(FLG_REALM2);
360                 if (MATCH_KEY(KEY_FIRST)) ADD_FLG(FLG_FIRST);
361                 if (MATCH_KEY(KEY_SECOND)) ADD_FLG(FLG_SECOND);
362                 if (MATCH_KEY(KEY_THIRD)) ADD_FLG(FLG_THIRD);
363                 if (MATCH_KEY(KEY_FOURTH)) ADD_FLG(FLG_FOURTH);
364         }
365
366         /* Not yet found any noun */
367         prev_flg = -1;
368
369         if (MATCH_KEY2(KEY_ARTIFACT)) ADD_FLG_NOUN(FLG_ARTIFACT);
370
371         if (MATCH_KEY2(KEY_ITEMS)) ADD_FLG_NOUN(FLG_ITEMS);
372         else if (MATCH_KEY2(KEY_WEAPONS)) ADD_FLG_NOUN(FLG_WEAPONS);
373         else if (MATCH_KEY2(KEY_ARMORS)) ADD_FLG_NOUN(FLG_ARMORS);
374         else if (MATCH_KEY2(KEY_MISSILES)) ADD_FLG_NOUN(FLG_MISSILES);
375         else if (MATCH_KEY2(KEY_DEVICES)) ADD_FLG_NOUN(FLG_DEVICES);
376         else if (MATCH_KEY2(KEY_LIGHTS)) ADD_FLG_NOUN(FLG_LIGHTS);
377         else if (MATCH_KEY2(KEY_JUNKS)) ADD_FLG_NOUN(FLG_JUNKS);
378         else if (MATCH_KEY2(KEY_SPELLBOOKS)) ADD_FLG_NOUN(FLG_SPELLBOOKS);
379         else if (MATCH_KEY2(KEY_HAFTED)) ADD_FLG_NOUN(FLG_HAFTED);
380         else if (MATCH_KEY2(KEY_SHIELDS)) ADD_FLG_NOUN(FLG_SHIELDS);
381         else if (MATCH_KEY2(KEY_BOWS)) ADD_FLG_NOUN(FLG_BOWS);
382         else if (MATCH_KEY2(KEY_RINGS)) ADD_FLG_NOUN(FLG_RINGS);
383         else if (MATCH_KEY2(KEY_AMULETS)) ADD_FLG_NOUN(FLG_AMULETS);
384         else if (MATCH_KEY2(KEY_SUITS)) ADD_FLG_NOUN(FLG_SUITS);
385         else if (MATCH_KEY2(KEY_CLOAKS)) ADD_FLG_NOUN(FLG_CLOAKS);
386         else if (MATCH_KEY2(KEY_HELMS)) ADD_FLG_NOUN(FLG_HELMS);
387         else if (MATCH_KEY2(KEY_GLOVES)) ADD_FLG_NOUN(FLG_GLOVES);
388         else if (MATCH_KEY2(KEY_BOOTS)) ADD_FLG_NOUN(FLG_BOOTS);
389         else if (MATCH_KEY2(KEY_FAVORITE)) ADD_FLG_NOUN(FLG_FAVORITE);
390
391         /* Last 'keyword' must be at the correct location */
392         if (*ptr == ':')
393                 ptr++;
394 #ifdef JP
395         else if (ptr[0] == kanji_colon[0] && ptr[1] == kanji_colon[1])
396                 ptr += 2;
397 #endif
398         else if (*ptr == '\0')
399         {
400                 /* There was no noun */
401                 if (prev_flg == -1)
402
403                 /* Add extra word "items" */
404                 ADD_FLG_NOUN(FLG_ITEMS);
405         }
406         else
407         {
408                 /* Noun type? */
409                 if (prev_flg != -1)
410                 {
411                         /* A noun type keyword didn't end correctly */
412                         entry->flag[prev_flg/32] &= ~(1L<< (prev_flg%32));
413                         ptr = prev_ptr;
414                 }
415         }
416
417         /* Save this auto-picker entry line */
418         entry->name = string_make(ptr);
419         entry->action = act;
420         entry->insc = string_make(insc);
421
422         return TRUE;
423 }
424
425
426 /*
427  * Favorite weapons
428  */
429 static bool is_favorite(object_type *o_ptr)
430 {
431         /* Only weapons match */
432         if (!(TV_WEAPON_BEGIN <= o_ptr->tval && o_ptr->tval <= TV_WEAPON_END))
433                 return FALSE;
434
435         /* Favorite weapons are varied depend on the class */
436         switch (p_ptr->pclass)
437         {
438         case CLASS_PRIEST:
439         {
440                 u32b flgs[TR_FLAG_SIZE];
441                 object_flags_known(o_ptr, flgs);
442
443                 if (!have_flag(flgs, TR_BLESSED) && 
444                     !(o_ptr->tval == TV_HAFTED))
445                         return FALSE;
446                 break;
447         }
448
449         case CLASS_MONK:
450         case CLASS_FORCETRAINER:
451                 /* Icky to wield? */
452                 if (!(s_info[p_ptr->pclass].w_max[o_ptr->tval-TV_WEAPON_BEGIN][o_ptr->sval]))
453                         return FALSE;
454                 break;
455
456         case CLASS_BEASTMASTER:
457         case CLASS_CAVALRY:
458         {
459                 u32b flgs[TR_FLAG_SIZE];
460                 object_flags_known(o_ptr, flgs);
461
462                 /* Is it known to be suitable to using while riding? */
463                 if (!(have_flag(flgs, TR_RIDING)))
464                         return FALSE;
465
466                 break;
467         }
468
469         case CLASS_NINJA:
470                 /* Icky to wield? */
471                 if (s_info[p_ptr->pclass].w_max[o_ptr->tval-TV_WEAPON_BEGIN][o_ptr->sval] <= WEAPON_EXP_BEGINNER)
472                         return FALSE;
473                 break;
474
475         default:
476                 /* All weapons are okay for non-special classes */
477                 return TRUE;
478         }
479
480         return TRUE;
481 }
482
483
484 /*
485  * Get auto-picker entry from o_ptr.
486  */
487 static void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
488 {
489         /* Assume that object name is to be added */
490         bool name = TRUE;
491
492         entry->name = NULL;
493         entry->insc = string_make(quark_str(o_ptr->inscription));
494         entry->action = DO_AUTOPICK | DO_DISPLAY;
495         entry->flag[0] = entry->flag[1] = 0L;
496         entry->dice = 0;
497
498         /* Unaware */
499         if (!object_aware_p(o_ptr))
500         {
501                 ADD_FLG(FLG_UNAWARE);
502         }
503
504         /* Not really identified */
505         else if (!object_known_p(o_ptr))
506         {
507                 if (!(o_ptr->ident & IDENT_SENSE))
508                 {
509                         ADD_FLG(FLG_UNIDENTIFIED);
510                 }
511                 else
512                 {
513                         /* Pseudo-identified */
514                         switch (o_ptr->feeling)
515                         {
516                         case FEEL_AVERAGE:
517                         case FEEL_GOOD:
518                                 ADD_FLG(FLG_NAMELESS);
519                                 break;
520
521                         case FEEL_BROKEN:
522                         case FEEL_CURSED:
523                                 ADD_FLG(FLG_NAMELESS);
524                                 ADD_FLG(FLG_WORTHLESS);
525                                 break;
526
527                         case FEEL_TERRIBLE:
528                         case FEEL_WORTHLESS:
529                                 ADD_FLG(FLG_WORTHLESS);
530                                 break;
531
532                         case FEEL_EXCELLENT:
533                                 ADD_FLG(FLG_EGO);
534                                 break;
535
536                         default:
537                                 /* It's not known to be useless so... */
538                                 ADD_FLG(FLG_UNIDENTIFIED);
539                                 break;
540                         }
541                 }
542         }
543
544         /* Identified */
545         else
546         {
547                 /* Ego objects */
548                 if (o_ptr->name2)
549                     
550                 {
551                         ego_item_type *e_ptr = &e_info[o_ptr->name2];
552                         entry->name = string_make(e_name + e_ptr->name);
553
554                         if (TV_WEAPON_BEGIN <= o_ptr->tval &&
555                             o_ptr->tval <= TV_ARMOR_END)
556                         {
557                                 /*
558                                  * Base name of ego weapons and armors
559                                  * are almost meaningless.  Ignore it.
560                                  */
561                                 name = FALSE;
562                         }
563
564                         ADD_FLG(FLG_EGO);
565                 }
566
567                 /* Artifact */
568                 else if (o_ptr->name1 || o_ptr->art_name)
569                         ADD_FLG(FLG_ARTIFACT);
570
571                 /* Non-ego, non-artifact */
572                 else
573                 {
574                         /* Wearable nameless object */
575                         if ((TV_EQUIP_BEGIN <= o_ptr->tval && o_ptr->tval <= TV_EQUIP_END))
576                                 ADD_FLG(FLG_NAMELESS);
577                 }
578
579         }
580
581
582         switch(o_ptr->tval)
583         {
584                 object_kind *k_ptr; 
585         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
586                 k_ptr = &k_info[o_ptr->k_idx];
587                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
588                         ADD_FLG(FLG_BOOSTED);
589         }
590
591         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
592         {
593                 REM_FLG(FLG_WORTHLESS);
594                 ADD_FLG(FLG_WANTED);
595         }
596
597         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
598             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
599         {
600                 ADD_FLG(FLG_UNIQUE);
601         }
602
603         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
604         {
605                 ADD_FLG(FLG_HUMAN);
606         }
607
608         if (o_ptr->tval >= TV_LIFE_BOOK &&
609             !check_book_realm(o_ptr->tval, o_ptr->sval))
610         {
611                 ADD_FLG(FLG_UNREADABLE);
612                 if (o_ptr->tval != TV_ARCANE_BOOK) name = FALSE;
613         }
614
615         if (REALM1_BOOK == o_ptr->tval &&
616             p_ptr->pclass != CLASS_SORCERER &&
617             p_ptr->pclass != CLASS_RED_MAGE)
618         {
619                 ADD_FLG(FLG_REALM1);
620                 name = FALSE;
621         }
622
623         if (REALM2_BOOK == o_ptr->tval &&
624             p_ptr->pclass != CLASS_SORCERER &&
625             p_ptr->pclass != CLASS_RED_MAGE)
626         {
627                 ADD_FLG(FLG_REALM2);
628                 name = FALSE;
629         }
630
631         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
632                 ADD_FLG(FLG_FIRST);
633         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
634                 ADD_FLG(FLG_SECOND);
635         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
636                 ADD_FLG(FLG_THIRD);
637         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
638                 ADD_FLG(FLG_FOURTH);
639
640         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
641                  || o_ptr->tval == TV_ARROW)
642                 ADD_FLG(FLG_MISSILES);
643         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
644                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
645                 ADD_FLG(FLG_DEVICES);
646         else if (o_ptr->tval == TV_LITE)
647                 ADD_FLG(FLG_LIGHTS);
648         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
649                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
650                 ADD_FLG(FLG_JUNKS);
651         else if (o_ptr->tval >= TV_LIFE_BOOK)
652                 ADD_FLG(FLG_SPELLBOOKS);
653         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
654                  || o_ptr->tval == TV_DIGGING || o_ptr->tval == TV_HAFTED)
655                 ADD_FLG(FLG_WEAPONS);
656         else if (o_ptr->tval == TV_SHIELD)
657                 ADD_FLG(FLG_SHIELDS);
658         else if (o_ptr->tval == TV_BOW)
659                 ADD_FLG(FLG_BOWS);
660         else if (o_ptr->tval == TV_RING)
661                 ADD_FLG(FLG_RINGS);
662         else if (o_ptr->tval == TV_AMULET)
663                 ADD_FLG(FLG_AMULETS);
664         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
665                  o_ptr->tval == TV_SOFT_ARMOR)
666                 ADD_FLG(FLG_SUITS);
667         else if (o_ptr->tval == TV_CLOAK)
668                 ADD_FLG(FLG_CLOAKS);
669         else if (o_ptr->tval == TV_HELM)
670                 ADD_FLG(FLG_HELMS);
671         else if (o_ptr->tval == TV_GLOVES)
672                 ADD_FLG(FLG_GLOVES);
673         else if (o_ptr->tval == TV_BOOTS)
674                 ADD_FLG(FLG_BOOTS);
675
676
677         if (name)
678         {
679                 char o_name[MAX_NLEN];
680                 object_desc(o_name, o_ptr, FALSE, 0);
681
682                 entry->name = string_make(o_name);
683         }
684
685         else if (!entry->name)
686         {
687                 entry->name = string_make("");
688         }
689
690         return;
691 }
692
693
694 /*
695  * A function to delete entry
696  */
697 static void autopick_free_entry(autopick_type *entry)
698 {
699         string_free(entry->name);
700         string_free(entry->insc);
701 }
702
703
704 /*
705  * Initialize auto-picker preference
706  */
707 #define MAX_AUTOPICK_DEFAULT 200
708
709 void init_autopicker(void)
710 {
711         static const char easy_autopick_inscription[] = "(:=g";
712         autopick_type entry;
713         int i;
714
715         if (!autopick_list)
716         {
717                 max_max_autopick = MAX_AUTOPICK_DEFAULT;
718                 C_MAKE(autopick_list, max_max_autopick, autopick_type);
719                 max_autopick = 0;
720         }
721
722         /* Clear old entries */
723         for( i = 0; i < max_autopick; i++)
724                 autopick_free_entry(&autopick_list[i]);
725
726         max_autopick = 0;
727
728         /* There is always one entry "=g" */
729         autopick_new_entry(&entry, easy_autopick_inscription, TRUE);
730         autopick_list[max_autopick++] = entry;
731 }
732
733
734 /*
735  * Add one line to autopick_list[]
736  */
737 static void add_autopick_list(autopick_type *entry)
738 {
739         /* There is no enough space to add one line */
740         if (max_autopick >= max_max_autopick)
741         {
742                 int old_max_max_autopick = max_max_autopick;
743                 autopick_type *old_autopick_list = autopick_list;
744
745                 /* Increase size of list */
746                 max_max_autopick += MAX_AUTOPICK_DEFAULT;
747
748                 /* Allocate */
749                 C_MAKE(autopick_list, max_max_autopick, autopick_type);
750
751                 /* Copy from old list to new list */
752                 C_COPY(autopick_list, old_autopick_list, old_max_max_autopick, autopick_type);
753
754                 /* Kill old list */
755                 C_FREE(old_autopick_list, old_max_max_autopick, autopick_type);
756         }
757
758         /* Add one line */
759         autopick_list[max_autopick] = *entry;
760
761         max_autopick++;
762 }
763
764
765 /*
766  *  Process line for auto picker/destroyer.
767  */
768 errr process_pickpref_file_line(char *buf)
769 {
770         autopick_type an_entry, *entry = &an_entry;
771         int i;
772
773         /* Nuke illegal char */
774         for(i = 0; buf[i]; i++)
775         {
776 #ifdef JP
777                 if (iskanji(buf[i]))
778                 {
779                         i++;
780                         continue;
781                 }
782 #endif
783                 if (isspace(buf[i]) && buf[i] != ' ')
784                         break;
785         }
786         buf[i] = 0;
787         
788         if (!autopick_new_entry(entry, buf, FALSE)) return 0;
789
790         /* Already has the same entry? */ 
791         for(i = 0; i < max_autopick; i++)
792                 if(!strcmp(entry->name, autopick_list[i].name)
793                    && entry->flag[0] == autopick_list[i].flag[0]
794                    && entry->flag[1] == autopick_list[i].flag[1]
795                    && entry->dice == autopick_list[i].dice
796                    && entry->bonus == autopick_list[i].bonus) return 0;
797
798         add_autopick_list(entry);
799         return 0;
800 }
801
802
803 /*
804  * Reconstruct preference line from entry
805  */
806 cptr autopick_line_from_entry(autopick_type *entry)
807 {
808         char buf[MAX_LINELEN];
809         char *ptr;
810         bool sepa_flag = TRUE;
811
812         *buf = '\0';
813         if (!(entry->action & DO_DISPLAY)) strcat(buf, "(");
814         if (entry->action & DO_QUERY_AUTOPICK) strcat(buf, ";");
815         if (entry->action & DO_AUTODESTROY) strcat(buf, "!");
816         if (entry->action & DONT_AUTOPICK) strcat(buf, "~");
817
818         ptr = buf;
819
820         if (IS_FLG(FLG_ALL)) ADD_KEY(KEY_ALL);
821         if (IS_FLG(FLG_COLLECTING)) ADD_KEY(KEY_COLLECTING);
822         if (IS_FLG(FLG_UNIDENTIFIED)) ADD_KEY(KEY_UNIDENTIFIED);
823         if (IS_FLG(FLG_IDENTIFIED)) ADD_KEY(KEY_IDENTIFIED);
824         if (IS_FLG(FLG_STAR_IDENTIFIED)) ADD_KEY(KEY_STAR_IDENTIFIED);
825         if (IS_FLG(FLG_UNAWARE)) ADD_KEY(KEY_UNAWARE);
826         if (IS_FLG(FLG_BOOSTED)) ADD_KEY(KEY_BOOSTED);
827
828         if (IS_FLG(FLG_MORE_THAN))
829         {
830                 ADD_KEY(KEY_MORE_THAN);
831                 strcat(ptr, format("%d", entry->dice));
832                 ADD_KEY(KEY_DICE);
833         }
834
835         if (IS_FLG(FLG_MORE_BONUS))
836         {
837                 ADD_KEY(KEY_MORE_BONUS);
838                 strcat(ptr, format("%d", entry->bonus));
839                 ADD_KEY(KEY_MORE_BONUS2);
840         }
841
842         if (IS_FLG(FLG_UNREADABLE)) ADD_KEY(KEY_UNREADABLE);
843         if (IS_FLG(FLG_REALM1)) ADD_KEY(KEY_REALM1);
844         if (IS_FLG(FLG_REALM2)) ADD_KEY(KEY_REALM2);
845         if (IS_FLG(FLG_FIRST)) ADD_KEY(KEY_FIRST);
846         if (IS_FLG(FLG_SECOND)) ADD_KEY(KEY_SECOND);
847         if (IS_FLG(FLG_THIRD)) ADD_KEY(KEY_THIRD);
848         if (IS_FLG(FLG_FOURTH)) ADD_KEY(KEY_FOURTH);
849         if (IS_FLG(FLG_WANTED)) ADD_KEY(KEY_WANTED);
850         if (IS_FLG(FLG_UNIQUE)) ADD_KEY(KEY_UNIQUE);
851         if (IS_FLG(FLG_HUMAN)) ADD_KEY(KEY_HUMAN);
852         if (IS_FLG(FLG_WORTHLESS)) ADD_KEY(KEY_WORTHLESS);
853         if (IS_FLG(FLG_NAMELESS)) ADD_KEY(KEY_NAMELESS);
854         if (IS_FLG(FLG_EGO)) ADD_KEY(KEY_EGO);
855
856         if (IS_FLG(FLG_ARTIFACT)) ADD_KEY(KEY_ARTIFACT);
857
858         if (IS_FLG(FLG_ITEMS)) ADD_KEY2(KEY_ITEMS);
859         else if (IS_FLG(FLG_WEAPONS)) ADD_KEY2(KEY_WEAPONS);
860         else if (IS_FLG(FLG_ARMORS)) ADD_KEY2(KEY_ARMORS);
861         else if (IS_FLG(FLG_MISSILES)) ADD_KEY2(KEY_MISSILES);
862         else if (IS_FLG(FLG_DEVICES)) ADD_KEY2(KEY_DEVICES);
863         else if (IS_FLG(FLG_LIGHTS)) ADD_KEY2(KEY_LIGHTS);
864         else if (IS_FLG(FLG_JUNKS)) ADD_KEY2(KEY_JUNKS);
865         else if (IS_FLG(FLG_SPELLBOOKS)) ADD_KEY2(KEY_SPELLBOOKS);
866         else if (IS_FLG(FLG_HAFTED)) ADD_KEY2(KEY_HAFTED);
867         else if (IS_FLG(FLG_SHIELDS)) ADD_KEY2(KEY_SHIELDS);
868         else if (IS_FLG(FLG_BOWS)) ADD_KEY2(KEY_BOWS);
869         else if (IS_FLG(FLG_RINGS)) ADD_KEY2(KEY_RINGS);
870         else if (IS_FLG(FLG_AMULETS)) ADD_KEY2(KEY_AMULETS);
871         else if (IS_FLG(FLG_SUITS)) ADD_KEY2(KEY_SUITS);
872         else if (IS_FLG(FLG_CLOAKS)) ADD_KEY2(KEY_CLOAKS);
873         else if (IS_FLG(FLG_HELMS)) ADD_KEY2(KEY_HELMS);
874         else if (IS_FLG(FLG_GLOVES)) ADD_KEY2(KEY_GLOVES);
875         else if (IS_FLG(FLG_BOOTS)) ADD_KEY2(KEY_BOOTS);
876         else if (IS_FLG(FLG_FAVORITE)) ADD_KEY2(KEY_FAVORITE);
877
878         /* You don't need sepalator after adjective */
879         /* 'artifact' is not true adjective */
880         else if (!IS_FLG(FLG_ARTIFACT))
881                 sepa_flag = FALSE;
882
883         if (entry->name && entry->name[0])
884         {
885                 int i, j = 0;
886
887                 if (sepa_flag) strcat(buf, ":");
888
889                 i = strlen(buf);
890                 while (entry->name[j] && i < MAX_LINELEN - 2 - 1)
891                 {
892 #ifdef JP
893                         if (iskanji(entry->name[j]))
894                                 buf[i++] = entry->name[j++];
895 #endif
896                         buf[i++] = entry->name[j++];
897                 }
898                 buf[i] = '\0';
899         }
900
901         if (entry->insc)
902         {
903                 int i, j = 0;
904                 strcat(buf, "#");
905                 i = strlen(buf);
906
907                 while (entry->insc[j] && i < MAX_LINELEN - 2)
908                 {
909 #ifdef JP
910                         if (iskanji(entry->insc[j]))
911                                 buf[i++] = entry->insc[j++];
912 #endif
913                         buf[i++] = entry->insc[j++];
914                 }
915                 buf[i] = '\0';
916         }
917
918         return string_make(buf);
919 }
920
921
922 /*
923  * Reconstruct preference line from entry and kill entry
924  */
925 static cptr autopick_line_from_entry_kill(autopick_type *entry)
926 {
927         cptr ptr = autopick_line_from_entry(entry);
928
929         /* Free memory for original entry */
930         autopick_free_entry(entry);
931
932         return ptr;
933 }
934
935
936 /*
937  * A function for Auto-picker/destroyer
938  * Examine whether the object matches to the entry
939  */
940 static bool is_autopick_aux(object_type *o_ptr, autopick_type *entry, cptr o_name)
941 {
942         int j;
943         cptr ptr = entry->name;
944
945         /*** Unidentified ***/
946         if (IS_FLG(FLG_UNIDENTIFIED)
947             && (object_known_p(o_ptr) || (o_ptr->ident & IDENT_SENSE)))
948                 return FALSE;
949
950         /*** Identified ***/
951         if (IS_FLG(FLG_IDENTIFIED) && !object_known_p(o_ptr))
952                 return FALSE;
953
954         /*** *Identified* ***/
955         if (IS_FLG(FLG_STAR_IDENTIFIED) &&
956             (!object_known_p(o_ptr) || !(o_ptr->ident & IDENT_MENTAL)))
957                 return FALSE;
958
959         /*** Dice boosted (weapon of slaying) ***/
960         if (IS_FLG(FLG_BOOSTED))
961         {
962                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
963                         
964                 switch( o_ptr->tval )
965                 {
966                 case TV_HAFTED:
967                 case TV_POLEARM:
968                 case TV_SWORD:
969                 case TV_DIGGING:
970                         if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
971                                 break;
972                         else
973                                 return FALSE;
974                 default:
975                         return FALSE;
976                 }
977         }
978
979         /*** Weapons which dd*ds is more than nn ***/
980         if (IS_FLG(FLG_MORE_THAN))
981         {
982                 if (o_ptr->dd * o_ptr->ds < entry->dice)
983                         return FALSE;
984         }
985                                 
986         /*** Weapons whic dd*ds is more than nn ***/
987         if (IS_FLG(FLG_MORE_BONUS))
988         {
989                 if (!object_known_p(o_ptr)) return FALSE;
990
991                 if (o_ptr->pval)
992                 {
993                         if (o_ptr->pval < entry->bonus) return FALSE;
994                 }
995                 else
996                 {
997                         if (o_ptr->to_h < entry->bonus &&
998                             o_ptr->to_d < entry->bonus &&
999                             o_ptr->to_a < entry->bonus &&
1000                             o_ptr->pval < entry->bonus)
1001                                 return FALSE;
1002                 }
1003         }
1004                                 
1005         /*** Worthless items ***/
1006         if (IS_FLG(FLG_WORTHLESS) && object_value(o_ptr) > 0)
1007                 return FALSE;
1008
1009         /*** Artifact object ***/
1010         if (IS_FLG(FLG_ARTIFACT))
1011         {
1012                 if (!object_known_p(o_ptr) || (!o_ptr->name1 && !o_ptr->art_name))
1013                         return FALSE;
1014         }
1015
1016         /*** Ego object ***/
1017         if (IS_FLG(FLG_EGO))
1018         {
1019                 /* Need to be an ego item */
1020                 if (!o_ptr->name2) return FALSE;
1021
1022                 /* Need to be known to be an ego */
1023                 if (!object_known_p(o_ptr) &&
1024                     !((o_ptr->ident & IDENT_SENSE) && o_ptr->feeling == FEEL_EXCELLENT))
1025                         return FALSE;
1026         }
1027
1028         /*** Nameless ***/
1029         if (IS_FLG(FLG_NAMELESS))
1030         {
1031                 if (!(TV_EQUIP_BEGIN <= o_ptr->tval && o_ptr->tval <= TV_EQUIP_END))
1032                         return FALSE;
1033
1034                 /* Inscription is a some sort of 'name' */
1035                 if (o_ptr->inscription) return FALSE;
1036
1037                 /* Identified */
1038                 if (object_known_p(o_ptr))
1039                 {
1040                         /* Artifacts and Ego objects are not okay */
1041                         if (o_ptr->name1 || o_ptr->art_name || o_ptr->name2)
1042                                 return FALSE;
1043                 }
1044
1045                 /* Pseudo-identified */
1046                 else if (o_ptr->ident & IDENT_SENSE)
1047                 {
1048                         switch (o_ptr->feeling)
1049                         {
1050                         case FEEL_AVERAGE:
1051                         case FEEL_GOOD:
1052                         case FEEL_BROKEN:
1053                         case FEEL_CURSED:
1054                                 /* It's nameless */
1055                                 break;
1056
1057                         default:
1058                                 /* It's not nameless */
1059                                 return FALSE;
1060                         }
1061                 }
1062
1063                 /* Unidentified */
1064                 else
1065                 {
1066                         /* Not known to be nameless */
1067                         return FALSE;
1068                 }
1069         }
1070
1071         /*** Unaware items ***/
1072         if (IS_FLG(FLG_UNAWARE) && object_aware_p(o_ptr))
1073                 return FALSE;
1074
1075         /*** Wanted monster's corpse/skeletons ***/
1076         if (IS_FLG(FLG_WANTED) &&
1077             (o_ptr->tval != TV_CORPSE || !object_is_shoukinkubi(o_ptr)))
1078                 return FALSE;
1079
1080         /*** Unique monster's corpse/skeletons/statues ***/
1081         if (IS_FLG(FLG_UNIQUE) &&
1082             ((o_ptr->tval != TV_CORPSE && o_ptr->tval != TV_STATUE) ||
1083              !(r_info[o_ptr->pval].flags1 & RF1_UNIQUE)))
1084                 return FALSE;
1085
1086         /*** Human corpse/skeletons (for Daemon magic) ***/
1087         if (IS_FLG(FLG_HUMAN) &&
1088             (o_ptr->tval != TV_CORPSE ||
1089              !strchr("pht", r_info[o_ptr->pval].d_char)))
1090                 return FALSE;
1091
1092         /*** Unreadable spellbooks ***/
1093         if (IS_FLG(FLG_UNREADABLE) &&
1094             (o_ptr->tval < TV_LIFE_BOOK ||
1095              check_book_realm(o_ptr->tval, o_ptr->sval)))
1096                 return FALSE;
1097
1098         /*** First realm spellbooks ***/
1099         if (IS_FLG(FLG_REALM1) && 
1100             (REALM1_BOOK != o_ptr->tval ||
1101              p_ptr->pclass == CLASS_SORCERER ||
1102              p_ptr->pclass == CLASS_RED_MAGE))
1103                 return FALSE;
1104
1105         /*** Second realm spellbooks ***/
1106         if (IS_FLG(FLG_REALM2) &&
1107             (REALM2_BOOK != o_ptr->tval ||
1108              p_ptr->pclass == CLASS_SORCERER ||
1109              p_ptr->pclass == CLASS_RED_MAGE))
1110                 return FALSE;
1111
1112         /*** First rank spellbooks ***/
1113         if (IS_FLG(FLG_FIRST) &&
1114             (o_ptr->tval < TV_LIFE_BOOK || 0 != o_ptr->sval))
1115                 return FALSE;
1116
1117         /*** Second rank spellbooks ***/
1118         if (IS_FLG(FLG_SECOND) &&
1119             (o_ptr->tval < TV_LIFE_BOOK || 1 != o_ptr->sval))
1120                 return FALSE;
1121
1122         /*** Third rank spellbooks ***/
1123         if (IS_FLG(FLG_THIRD) && 
1124             (o_ptr->tval < TV_LIFE_BOOK || 2 != o_ptr->sval))
1125                 return FALSE;
1126
1127         /*** Fourth rank spellbooks ***/
1128         if (IS_FLG(FLG_FOURTH) &&
1129             (o_ptr->tval < TV_LIFE_BOOK || 3 != o_ptr->sval))
1130                 return FALSE;
1131
1132         /*** Items ***/
1133         if (IS_FLG(FLG_WEAPONS))
1134         {
1135                 if (!(TV_WEAPON_BEGIN <= o_ptr->tval && o_ptr->tval <= TV_WEAPON_END))
1136                         return FALSE;
1137         }
1138         else if (IS_FLG(FLG_ARMORS))
1139         {
1140                 if (!(TV_ARMOR_BEGIN <= o_ptr->tval && o_ptr->tval <= TV_ARMOR_END))
1141                         return FALSE;
1142         }
1143         else if (IS_FLG(FLG_MISSILES))
1144         {
1145                 switch(o_ptr->tval)
1146                 {
1147                 case TV_SHOT: case TV_BOLT: case TV_ARROW:
1148                         break;
1149                 default: return FALSE;
1150                 }
1151         }
1152         else if (IS_FLG(FLG_DEVICES))
1153         {
1154                 switch(o_ptr->tval)
1155                 {
1156                 case TV_SCROLL: case TV_STAFF: case TV_WAND: case TV_ROD:
1157                         break;
1158                 default: return FALSE;
1159                 }
1160         }
1161         else if (IS_FLG(FLG_LIGHTS))
1162         {
1163                 if (!(o_ptr->tval == TV_LITE))
1164                         return FALSE;
1165         }
1166         else if (IS_FLG(FLG_JUNKS))
1167         {
1168                 switch(o_ptr->tval)
1169                 {
1170                 case TV_SKELETON: case TV_BOTTLE:
1171                 case TV_JUNK: case TV_STATUE:
1172                         break;
1173                 default: return FALSE;
1174                 }
1175         }
1176         else if (IS_FLG(FLG_SPELLBOOKS))
1177         {
1178                 if (!(o_ptr->tval >= TV_LIFE_BOOK))
1179                         return FALSE;
1180         }
1181         else if (IS_FLG(FLG_HAFTED))
1182         {
1183                 if (!(o_ptr->tval == TV_HAFTED))
1184                         return FALSE;
1185         }
1186         else if (IS_FLG(FLG_SHIELDS))
1187         {
1188                 if (!(o_ptr->tval == TV_SHIELD))
1189                         return FALSE;
1190         }
1191         else if (IS_FLG(FLG_BOWS))
1192         {
1193                 if (!(o_ptr->tval == TV_BOW))
1194                         return FALSE;
1195         }
1196         else if (IS_FLG(FLG_RINGS))
1197         {
1198                 if (!(o_ptr->tval == TV_RING))
1199                         return FALSE;
1200         }
1201         else if (IS_FLG(FLG_AMULETS))
1202         {
1203                 if (!(o_ptr->tval == TV_AMULET))
1204                         return FALSE;
1205         }
1206         else if (IS_FLG(FLG_SUITS))
1207         {
1208                 if (!(o_ptr->tval == TV_DRAG_ARMOR ||
1209                       o_ptr->tval == TV_HARD_ARMOR ||
1210                       o_ptr->tval == TV_SOFT_ARMOR))
1211                         return FALSE;
1212         }
1213         else if (IS_FLG(FLG_CLOAKS))
1214         {
1215                 if (!(o_ptr->tval == TV_CLOAK))
1216                         return FALSE;
1217         }
1218         else if (IS_FLG(FLG_HELMS))
1219         {
1220                 if (!(o_ptr->tval == TV_CROWN || o_ptr->tval == TV_HELM))
1221                         return FALSE;
1222         }
1223         else if (IS_FLG(FLG_GLOVES))
1224         {
1225                 if (!(o_ptr->tval == TV_GLOVES))
1226                         return FALSE;
1227         }
1228         else if (IS_FLG(FLG_BOOTS))
1229         {
1230                 if (!(o_ptr->tval == TV_BOOTS))
1231                         return FALSE;
1232         }
1233         else if (IS_FLG(FLG_FAVORITE))
1234         {
1235                 if (!is_favorite(o_ptr))
1236                         return FALSE;
1237         }
1238
1239         /* Keyword don't match */
1240         if (*ptr == '^')
1241         {
1242                 ptr++;
1243                 if (strncmp(o_name, ptr, strlen(ptr))) return FALSE;
1244         }
1245         else
1246         {
1247 #ifdef JP
1248                 if (!strstr_j(o_name, ptr)) return FALSE;
1249 #else
1250                 if (!strstr(o_name, ptr)) return FALSE;
1251 #endif
1252         }
1253
1254         /* TRUE when it need not to be 'collecting' */
1255         if (!IS_FLG(FLG_COLLECTING)) return TRUE;
1256
1257         /* Check if there is a same item */
1258         for (j = 0; j < INVEN_PACK; j++)
1259         {
1260                 /*
1261                  * 'Collecting' means the item must be absorbed 
1262                  * into an inventory slot.
1263                  * But an item can not be absorbed into itself!
1264                  */
1265                 if ((&inventory[j] != o_ptr) &&
1266                     object_similar(&inventory[j], o_ptr))
1267                         return TRUE;
1268         }
1269
1270         /* Not collecting */
1271         return FALSE;
1272 }
1273
1274
1275 /*
1276  * A function for Auto-picker/destroyer
1277  * Examine whether the object matches to the list of keywords or not.
1278  */
1279 int is_autopick(object_type *o_ptr)
1280 {
1281         int i;
1282         char o_name[MAX_NLEN];
1283
1284         if (o_ptr->tval == TV_GOLD) return -1;
1285         
1286         object_desc(o_name, o_ptr, FALSE, 3);
1287
1288         /* Force to be lower case string */
1289         for (i = 0; o_name[i]; i++)
1290         {
1291 #ifdef JP
1292                 if (iskanji(o_name[i]))
1293                         i++;
1294                 else
1295 #endif
1296                 if (isupper(o_name[i]))
1297                         o_name[i] = tolower(o_name[i]);
1298         }
1299         
1300         for (i=0; i < max_autopick; i++)
1301         {
1302                 autopick_type *entry = &autopick_list[i];
1303
1304                 if (is_autopick_aux(o_ptr, entry, o_name)) return i;
1305         }
1306
1307         /* No matching entry */
1308         return -1;
1309 }
1310
1311
1312 /*
1313  *  Auto inscription
1314  */
1315 void auto_inscribe_item(int item, int idx)
1316 {
1317         object_type *o_ptr;
1318
1319         /* Get the item (in the pack) */
1320         if (item >= 0) o_ptr = &inventory[item];
1321
1322         /* Get the item (on the floor) */
1323         else o_ptr = &o_list[0 - item];
1324
1325         /* Auto-inscription or Re-inscribe for resistances {%} */
1326         if ((idx < 0 || !autopick_list[idx].insc) && !o_ptr->inscription)
1327                 return;
1328
1329         if (!o_ptr->inscription)
1330                 o_ptr->inscription = quark_add(autopick_list[idx].insc);
1331
1332         if (item > INVEN_PACK)
1333         {
1334                 /* Redraw inscription */
1335                 p_ptr->window |= (PW_EQUIP);
1336
1337                 /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
1338                 p_ptr->update |= (PU_BONUS);
1339         }
1340         else if (item >= 0)
1341         {
1342                 /* Redraw inscription */
1343                 p_ptr->window |= (PW_INVEN);
1344         }
1345 }
1346
1347
1348 /*
1349  * Automatically destroy items in this grid.
1350  */
1351 static bool is_opt_confirm_destroy(object_type *o_ptr)
1352 {
1353         if (!destroy_items) return FALSE;
1354
1355         /* Known to be worthless? */
1356         if (leave_worth)
1357                 if (object_value(o_ptr) > 0) return FALSE;
1358         
1359         if (leave_equip)
1360                 if ((o_ptr->tval >= TV_MISSILE_BEGIN) && (o_ptr->tval <= TV_ARMOR_END)) return FALSE;
1361         
1362         if (leave_chest)
1363                 if ((o_ptr->tval == TV_CHEST) && o_ptr->pval) return FALSE;
1364         
1365         if (leave_wanted)
1366         {
1367                 if (o_ptr->tval == TV_CORPSE
1368                     && object_is_shoukinkubi(o_ptr)) return FALSE;
1369         }
1370         
1371         if (leave_corpse)
1372                 if (o_ptr->tval == TV_CORPSE) return FALSE;
1373         
1374         if (leave_junk)
1375                 if ((o_ptr->tval == TV_SKELETON) || (o_ptr->tval == TV_BOTTLE) || (o_ptr->tval == TV_JUNK) || (o_ptr->tval == TV_STATUE)) return FALSE;
1376
1377         if (leave_special)
1378         {
1379                 if (p_ptr->prace == RACE_DEMON)
1380                 {
1381                         if (o_ptr->tval == TV_CORPSE &&
1382                             o_ptr->sval == SV_CORPSE &&
1383                             strchr("pht", r_info[o_ptr->pval].d_char))
1384                                 return FALSE;
1385                 }
1386
1387                 if (p_ptr->pclass == CLASS_ARCHER)
1388                 {
1389                         if (o_ptr->tval == TV_SKELETON ||
1390                             (o_ptr->tval == TV_CORPSE && o_ptr->sval == SV_SKELETON))
1391                                 return FALSE;
1392                 }
1393                 else if (p_ptr->pclass == CLASS_NINJA)
1394                 {
1395                         if (o_ptr->tval == TV_LITE &&
1396                             o_ptr->name2 == EGO_LITE_DARKNESS)
1397                                 return FALSE;
1398                 }
1399                 else if (p_ptr->pclass == CLASS_BEASTMASTER ||
1400                          p_ptr->pclass == CLASS_CAVALRY)
1401                 {
1402                         if (o_ptr->tval == TV_WAND &&
1403                             o_ptr->sval == SV_WAND_HEAL_MONSTER)
1404                                 return FALSE;
1405                 }
1406         }
1407         
1408         if (o_ptr->tval == TV_GOLD) return FALSE;
1409         
1410         return TRUE;
1411 }
1412
1413
1414 /*
1415  * Automatically destroy an item if it is to be destroyed
1416  */
1417 static object_type autopick_last_destroyed_object;
1418
1419 bool auto_destroy_item(int item, int autopick_idx)
1420 {
1421         bool destroy = FALSE;
1422         object_type *o_ptr;
1423
1424         /* Don't destroy equipped items */
1425         if (item > INVEN_PACK) return FALSE;
1426
1427         /* Get the item (in the pack) */
1428         if (item >= 0) o_ptr = &inventory[item];
1429
1430         /* Get the item (on the floor) */
1431         else o_ptr = &o_list[0 - item];
1432
1433         /* Easy-Auto-Destroyer */
1434         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
1435
1436         /* Protected by auto-picker */
1437         if (autopick_idx >= 0 &&
1438             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
1439                 destroy = FALSE;
1440
1441         if (!always_pickup)
1442         {
1443                 /* Auto-picker/destroyer */
1444                 if (autopick_idx >= 0 &&
1445                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
1446                         destroy = TRUE;
1447         }
1448
1449         /* Not to be destroyed */
1450         if (!destroy) return FALSE;
1451
1452         /* Now decided to destroy */
1453
1454         disturb(0,0);
1455
1456         /* Artifact? */
1457         if (!can_player_destroy_object(o_ptr))
1458         {
1459                 char o_name[MAX_NLEN];
1460
1461                 /* Describe the object (with {terrible/special}) */
1462                 object_desc(o_name, o_ptr, TRUE, 3);
1463
1464                 /* Message */
1465 #ifdef JP
1466                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
1467 #else
1468                 msg_format("You cannot auto-destroy %s.", o_name);
1469 #endif
1470
1471                 /* Done */
1472                 return TRUE;
1473         }
1474
1475         /* Record name of destroyed item */
1476         COPY(&autopick_last_destroyed_object, o_ptr, object_type);
1477
1478         /* Destroy Later */
1479         o_ptr->marked |= OM_AUTODESTROY;
1480         p_ptr->notice |= PN_AUTODESTROY;
1481
1482         return TRUE;
1483 }
1484
1485
1486 /*
1487  *  Auto-destroy marked item
1488  */
1489 static void delayed_auto_destroy_aux(int item)
1490 {
1491         object_type *o_ptr;
1492
1493         /* Get the item (in the pack) */
1494         if (item >= 0) o_ptr = &inventory[item];
1495
1496         /* Get the item (on the floor) */
1497         else o_ptr = &o_list[0 - item];
1498
1499         if (o_ptr->k_idx && o_ptr->marked & OM_AUTODESTROY)
1500         {
1501                 char o_name[MAX_NLEN];
1502
1503                 /* Describe the object (with {terrible/special}) */
1504                 object_desc(o_name, o_ptr, TRUE, 3);
1505
1506                 /* Eliminate the item (from the pack) */
1507                 if (item >= 0)
1508                 {
1509                         inven_item_increase(item, -(o_ptr->number));
1510                         inven_item_optimize(item);
1511                 }
1512
1513                 /* Eliminate the item (from the floor) */
1514                 else
1515                 {
1516                         delete_object_idx(0 - item);
1517                 }
1518
1519                 /* Print a message */
1520 #ifdef JP
1521                 msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
1522 #else
1523                 msg_format("Auto-destroying %s.", o_name);
1524 #endif
1525         }
1526 }
1527
1528
1529 /*
1530  *  Auto-destroy marked item in inventry and on floor
1531  */
1532 void delayed_auto_destroy(void)
1533 {
1534         int item;
1535
1536         /* 
1537          * Scan inventry in reverse order to prevent
1538          * skipping after inven_item_optimize()
1539          */
1540         for (item = INVEN_TOTAL - 1; item >= 0 ; item--)
1541                 delayed_auto_destroy_aux(item);
1542
1543         /* Scan the pile of objects */
1544         item = cave[py][px].o_idx;
1545         while (item)
1546         {
1547                 int next = o_list[item].next_o_idx;
1548                 delayed_auto_destroy_aux(-item);
1549                 item = next;
1550         }
1551 }
1552
1553
1554 /*
1555  * Automatically pickup/destroy items in this grid.
1556  */
1557 void auto_pickup_items(cave_type *c_ptr)
1558 {
1559         s16b this_o_idx, next_o_idx = 0;
1560         
1561         /* Scan the pile of objects */
1562         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1563         {
1564                 int idx;
1565         
1566                 /* Acquire object */
1567                 object_type *o_ptr = &o_list[this_o_idx];
1568                 
1569                 /* Acquire next object */
1570                 next_o_idx = o_ptr->next_o_idx;
1571
1572                 idx = is_autopick(o_ptr);
1573
1574                 /* Item index for floor -1,-2,-3,...  */
1575                 auto_inscribe_item((-this_o_idx), idx);
1576
1577                 if (idx >= 0 &&
1578                         (autopick_list[idx].action & (DO_AUTOPICK | DO_QUERY_AUTOPICK)))
1579                 {
1580                         disturb(0,0);
1581
1582                         if (!inven_carry_okay(o_ptr))
1583                         {
1584                                 char o_name[MAX_NLEN];
1585
1586                                 /* Describe the object */
1587                                 object_desc(o_name, o_ptr, TRUE, 3);
1588
1589                                 /* Message */
1590 #ifdef JP
1591                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1592 #else
1593                                 msg_format("You have no room for %s.", o_name);
1594 #endif
1595                                 /* Hack - remember that the item has given a message here. */
1596                                 o_ptr->marked |= OM_NOMSG;
1597
1598                                 continue;
1599                         }
1600                         else if (autopick_list[idx].action & DO_QUERY_AUTOPICK)
1601                         {
1602                                 char out_val[MAX_NLEN+20];
1603                                 char o_name[MAX_NLEN];
1604
1605                                 if (o_ptr->marked & OM_NO_QUERY)
1606                                 {
1607                                         /* Already answered as 'No' */
1608                                         continue;
1609                                 }
1610
1611                                 /* Describe the object */
1612                                 object_desc(o_name, o_ptr, TRUE, 3);
1613
1614 #ifdef JP
1615                                 sprintf(out_val, "%s¤ò½¦¤¤¤Þ¤¹¤«? ", o_name);
1616 #else
1617                                 sprintf(out_val, "Pick up %s? ", o_name);
1618 #endif
1619
1620                                 if (!get_check(out_val))
1621                                 {
1622                                         /* Hack - remember that the item has given a message here. */
1623                                         o_ptr->marked |= (OM_NOMSG | OM_NO_QUERY);
1624                                         continue;
1625                                 }
1626
1627                         }
1628                         py_pickup_aux(this_o_idx);
1629
1630                         continue;
1631                 }
1632                 
1633                 /*
1634                  * Do auto-destroy;
1635                  * When always_pickup is 'yes', we disable
1636                  * auto-destroyer from autopick function, and do only
1637                  * easy-auto-destroyer.
1638                  */
1639                 else
1640                 {
1641                         if (auto_destroy_item((-this_o_idx), idx))
1642                                 continue;
1643                 }
1644         }
1645 }
1646
1647
1648 #define PT_DEFAULT 0
1649 #define PT_WITH_PNAME 1
1650
1651 /*
1652  *  Get file name for autopick preference
1653  */
1654 static cptr pickpref_filename(int filename_mode)
1655 {
1656 #ifdef JP
1657         static const char namebase[] = "picktype";
1658 #else
1659         static const char namebase[] = "pickpref";
1660 #endif
1661
1662         switch (filename_mode)
1663         {
1664         case PT_DEFAULT:
1665                 return format("%s.prf", namebase);
1666
1667         case PT_WITH_PNAME:
1668                 return format("%s-%s.prf", namebase, player_name);
1669
1670         default:
1671                 return NULL;
1672         }
1673 }
1674
1675
1676 static const char autoregister_header[] = "?:$AUTOREGISTER";
1677
1678 /*
1679  *  Clear auto registered lines in the picktype.prf .
1680  */
1681 static bool clear_auto_register(void)
1682 {
1683         char tmp_file[1024];
1684         char pref_file[1024];
1685         char buf[1024];
1686         FILE *pref_fff;
1687         FILE *tmp_fff;
1688         int num = 0;
1689         bool autoregister = FALSE;
1690         bool okay = TRUE;
1691
1692         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1693         pref_fff = my_fopen(pref_file, "r");
1694
1695         if (!pref_fff)
1696         {
1697                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1698                 pref_fff = my_fopen(pref_file, "r");
1699         }
1700
1701         if (!pref_fff)
1702         {
1703                 /* No file yet */
1704                 return TRUE;
1705         }
1706
1707         /* Open a new (temporary) file */
1708         tmp_fff = my_fopen_temp(tmp_file, sizeof(tmp_file));
1709
1710         if (!tmp_fff)
1711         {
1712                 /* Close the preference file */
1713                 fclose(pref_fff);
1714
1715 #ifdef JP
1716                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", tmp_file);
1717 #else
1718                 msg_format("Failed to create temporary file %s.", tmp_file);
1719 #endif
1720                 msg_print(NULL);
1721                 return FALSE;
1722         }
1723
1724         
1725         /* Loop for every line */
1726         while (TRUE)
1727         {
1728                 /* Read a line */
1729                 if (my_fgets(pref_fff, buf, sizeof(buf))) break;
1730
1731                 if (autoregister)
1732                 {
1733                         /* Delete auto-registered line */
1734
1735                         /* Count auto-destroy preference lines */
1736                         if (buf[0] != '#' && buf[0] != '?') num++;
1737                 }
1738
1739                 /* We are looking for auto-registered line */
1740                 else
1741                 {
1742                         if (streq(buf, autoregister_header))
1743                         {
1744                                 /* Delete all further lines */
1745                                 autoregister = TRUE;
1746                         }
1747                         else
1748                         {
1749                                 /* Copy orginally lines */
1750                                 fprintf(tmp_fff, "%s\n", buf);
1751                         }
1752                 }
1753         }
1754
1755         /* Close files */
1756         my_fclose(pref_fff);
1757         my_fclose(tmp_fff);
1758
1759         if (num)
1760         {
1761 #ifdef JP
1762                 msg_format("°ÊÁ°¤Î¥­¥ã¥é¥¯¥¿¡¼ÍѤμ«Æ°ÀßÄê(%d¹Ô)¤¬»Ä¤Ã¤Æ¤¤¤Þ¤¹¡£", num);
1763                 strcpy(buf, "¸Å¤¤ÀßÄê¹Ô¤Ïºï½ü¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©");
1764 #else
1765                 msg_format("Auto registered lines (%d lines) for previous character are remaining.", num);
1766                 strcpy(buf, "These lines will be deleted.  Are you sure? ");
1767 #endif
1768
1769                 /* You can cancel it */
1770                 if (!get_check(buf))
1771                 {
1772                         okay = FALSE;
1773                         autoregister = FALSE;
1774
1775 #ifdef JP
1776                         msg_print("¥¨¥Ç¥£¥¿¤Î¥«¥Ã¥È&¥Ú¡¼¥¹¥ÈÅù¤ò»È¤Ã¤ÆɬÍפʹԤòÈòÆñ¤·¤Æ¤¯¤À¤µ¤¤¡£");
1777 #else
1778                         msg_print("Use cut & paste of auto picker editor (_) to keep old prefs.");
1779 #endif
1780                 }
1781         }
1782
1783
1784         /* If there are some changes, overwrite the original file with new one */
1785         if (autoregister)
1786         {
1787                 /* Copy contents of temporary file */
1788
1789                 tmp_fff = my_fopen(tmp_file, "r");
1790                 pref_fff = my_fopen(pref_file, "w");
1791
1792                 while (!my_fgets(tmp_fff, buf, sizeof(buf)))
1793                         fprintf(pref_fff, "%s\n", buf);
1794
1795                 my_fclose(pref_fff);
1796                 my_fclose(tmp_fff);
1797         }
1798
1799         /* Kill the temporary file */
1800         fd_kill(tmp_file);
1801
1802         return okay;
1803 }
1804
1805
1806 /*
1807  *  Automatically register an auto-destroy preference line
1808  */
1809 bool add_auto_register(object_type *o_ptr)
1810 {
1811         char buf[1024];
1812         char pref_file[1024];
1813         FILE *pref_fff;
1814         autopick_type an_entry, *entry = &an_entry;
1815
1816         int match_autopick = is_autopick(o_ptr);
1817
1818         /* Already registered */
1819         if (match_autopick != -1)
1820         {
1821                 cptr what;
1822                 byte act = autopick_list[match_autopick].action;
1823
1824 #ifdef JP
1825                 if (act & DO_AUTOPICK) what = "¼«Æ°¤Ç½¦¤¦";
1826                 else if (act & DO_AUTODESTROY) what = "¼«Æ°Ç˲õ¤¹¤ë";
1827                 else if (act & DONT_AUTOPICK) what = "ÊüÃÖ¤¹¤ë";
1828                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "³Îǧ¤·¤Æ½¦¤¦";
1829
1830                 msg_format("¤½¤Î¥¢¥¤¥Æ¥à¤Ï´û¤Ë%s¤è¤¦¤ËÀßÄꤵ¤ì¤Æ¤¤¤Þ¤¹¡£", what);
1831 #else
1832                 if (act & DO_AUTOPICK) what = "auto-pickup";
1833                 else if (act & DO_AUTODESTROY) what = "auto-destroy";
1834                 else if (act & DONT_AUTOPICK) what = "leave on floor";
1835                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "query auto-pickup";
1836
1837                 msg_format("The object is already registered to %s.", what);
1838 #endif
1839                 
1840                 return FALSE;
1841         }
1842
1843
1844         if (!p_ptr->autopick_autoregister)
1845         {
1846                 /* Clear old auto registered lines */
1847                 if (!clear_auto_register()) return FALSE;
1848         }
1849
1850         /* Try a filename with player name */
1851         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1852         pref_fff = my_fopen(pref_file, "r");
1853
1854         if (!pref_fff)
1855         {
1856                 /* Use default name */
1857                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1858                 pref_fff = my_fopen(pref_file, "r");
1859         }
1860
1861         /* Check the header */
1862         while (TRUE)
1863         {
1864                 /* Read a line */
1865                 if (my_fgets(pref_fff, buf, sizeof(buf)))
1866                 {
1867                         /* No header found */
1868                         p_ptr->autopick_autoregister = FALSE;
1869
1870                         break;
1871                 }
1872
1873                 if (streq(buf, autoregister_header))
1874                 {
1875                         /* Found the header */
1876                         p_ptr->autopick_autoregister = TRUE;
1877
1878                         break;
1879                 }
1880         }
1881
1882         /* Close read only FILE* */
1883         fclose(pref_fff);
1884
1885         /* Open for append */
1886         pref_fff = my_fopen(pref_file, "a");
1887
1888         /* Failure */
1889         if (!pref_fff) {
1890 #ifdef JP
1891                 msg_format("%s ¤ò³«¤¯¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", pref_file);
1892 #else
1893                 msg_format("Failed to open %s.", pref_file);
1894 #endif
1895                 msg_print(NULL);
1896
1897                 /* Failed */
1898                 return FALSE;
1899         }
1900
1901         if (!p_ptr->autopick_autoregister)
1902         {
1903                 /* Add the header */
1904                 fprintf(pref_fff, "%s\n", autoregister_header);
1905
1906 #ifdef JP
1907                 fprintf(pref_fff, "%s\n", "# *·Ù¹ð!!* °Ê¹ß¤Î¹Ô¤Ï¼«Æ°ÅÐÏ¿¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£");
1908                 fprintf(pref_fff, "%s\n", "# ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢É¬ÍפʹԤϾå¤ÎÊý¤Ø°ÜÆ°¤·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£");
1909 #else
1910                 fprintf(pref_fff, "%s\n", "# *Waring!* The lines below will be deleated later.");
1911                 fprintf(pref_fff, "%s\n", "# Keep it by cut & paste if you need these lines for future characters.");
1912 #endif
1913
1914                 /* Now auto register is in-use */
1915                 p_ptr->autopick_autoregister = TRUE;
1916         }
1917
1918         /* Get a preference entry */
1919         autopick_entry_from_object(entry, o_ptr);
1920
1921         /* Set to auto-destroy (with no-display) */
1922         entry->action = DO_AUTODESTROY;
1923
1924         /* Load the new line as preference */
1925         add_autopick_list(entry);
1926
1927         /* Add a line to the file */
1928         /* Don't kill "entry" */
1929         fprintf(pref_fff, "%s\n", autopick_line_from_entry(entry));
1930
1931         /* Close the file */
1932         fclose(pref_fff);
1933
1934         return TRUE;
1935 }
1936
1937
1938 /********  Auto-picker/destroyer editor  **********/
1939
1940 #define MAX_YANK MAX_LINELEN
1941 #define MAX_LINES 3000
1942
1943 #define MARK_MARK     0x01
1944 #define MARK_BY_SHIFT 0x02
1945
1946 #define LSTAT_BYPASS        0x01
1947 #define LSTAT_EXPRESSION    0x02
1948 #define LSTAT_AUTOREGISTER  0x04
1949
1950 #define QUIT_WITHOUT_SAVE 1
1951 #define QUIT_AND_SAVE     2
1952
1953 /* 
1954  * Struct for yank buffer
1955  */
1956 typedef struct chain_str {
1957         struct chain_str *next;
1958         char s[1];
1959 } chain_str_type;
1960
1961
1962 /*
1963  * Data struct for text editor
1964  */
1965 typedef struct {
1966         int wid, hgt;
1967         int cx, cy;
1968         int upper, left;
1969         int old_wid, old_hgt;
1970         int old_cy;
1971         int old_upper, old_left;
1972         int mx, my;
1973         byte mark;
1974
1975         object_type *search_o_ptr;
1976         cptr search_str;
1977         cptr last_destroyed;
1978
1979         chain_str_type *yank;
1980         bool yank_eol;
1981
1982         cptr *lines_list;
1983         byte states[MAX_LINES];
1984
1985         byte dirty_flags;
1986         int dirty_line;
1987         int filename_mode;
1988         int old_com_id;
1989
1990         bool changed;
1991 } text_body_type;
1992
1993
1994 /*
1995  * Dirty flag for text editor
1996  */
1997 #define DIRTY_ALL        0x01
1998 #define DIRTY_MODE       0x04
1999 #define DIRTY_SCREEN     0x08
2000 #define DIRTY_NOT_FOUND  0x10
2001 #define DIRTY_NO_SEARCH  0x20
2002 #define DIRTY_EXPRESSION 0x40
2003
2004
2005 /*
2006  * Describe which kind of object is Auto-picked/destroyed
2007  */
2008 static void describe_autopick(char *buff, autopick_type *entry)
2009 {
2010         cptr str = entry->name;
2011         byte act = entry->action;
2012         cptr insc = entry->insc;
2013         int i;
2014
2015         bool top = FALSE;
2016
2017 #ifdef JP
2018         cptr before_str[100], body_str;
2019         int before_n = 0;
2020
2021         body_str = "¥¢¥¤¥Æ¥à";
2022
2023         /*** Collecting items ***/
2024         /*** Which can be absorbed into a slot as a bundle ***/
2025         if (IS_FLG(FLG_COLLECTING))
2026                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
2027         
2028         /*** Unidentified ***/
2029         if (IS_FLG(FLG_UNIDENTIFIED))
2030                 before_str[before_n++] = "̤´ÕÄê¤Î";
2031
2032         /*** Identified ***/
2033         if (IS_FLG(FLG_IDENTIFIED))
2034                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
2035
2036         /*** *Identified* ***/
2037         if (IS_FLG(FLG_STAR_IDENTIFIED))
2038                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
2039
2040         /*** Dice boosted (weapon of slaying) ***/
2041         if (IS_FLG(FLG_BOOSTED))
2042         {
2043                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
2044                 body_str = "Éð´ï";
2045         }
2046
2047         /*** Weapons whose dd*ds is more than nn ***/
2048         if (IS_FLG(FLG_MORE_THAN))
2049         {
2050                 static char more_than_desc_str[] = "___";
2051                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
2052                 body_str = "Éð´ï";
2053                         
2054                 sprintf(more_than_desc_str,"%d", entry->dice);
2055                 before_str[before_n++] = more_than_desc_str;
2056                 before_str[before_n++] = "°Ê¾å¤Î";
2057         }
2058
2059         /*** Items whose magical bonus is more than nn ***/
2060         if (IS_FLG(FLG_MORE_BONUS))
2061         {
2062                 static char more_bonus_desc_str[] = "___";
2063                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
2064                         
2065                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
2066                 before_str[before_n++] = more_bonus_desc_str;
2067                 before_str[before_n++] = ")°Ê¾å¤Î";
2068         }
2069
2070         /*** Worthless items ***/
2071         if (IS_FLG(FLG_WORTHLESS))
2072                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
2073
2074         /*** Artifact ***/
2075         if (IS_FLG(FLG_ARTIFACT))
2076         {
2077                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
2078                 body_str = "ÁõÈ÷";
2079         }
2080
2081         /*** Ego ***/
2082         if (IS_FLG(FLG_EGO))
2083         {
2084                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
2085                 body_str = "ÁõÈ÷";
2086         }
2087
2088         /*** Nameless ***/
2089         if (IS_FLG(FLG_NAMELESS))
2090         {
2091                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
2092                 body_str = "ÁõÈ÷";
2093         }
2094
2095         /*** Unaware items ***/
2096         if (IS_FLG(FLG_UNAWARE))
2097                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
2098
2099         /*** Wanted monster's corpse/skeletons ***/
2100         if (IS_FLG(FLG_WANTED))
2101         {
2102                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
2103                 body_str = "»àÂΤä¹ü";
2104         }
2105
2106         /*** Human corpse/skeletons (for Daemon magic) ***/
2107         if (IS_FLG(FLG_HUMAN))
2108         {
2109                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
2110                 body_str = "»àÂΤä¹ü";
2111         }
2112
2113         /*** Unique monster's corpse/skeletons/statues ***/
2114         if (IS_FLG(FLG_UNIQUE))
2115         {
2116                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
2117                 body_str = "»àÂΤä¹ü";
2118         }
2119
2120         /*** Unreadable spellbooks ***/
2121         if (IS_FLG(FLG_UNREADABLE))
2122         {
2123                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
2124                 body_str = "ËâË¡½ñ";
2125         }
2126
2127         /*** First realm spellbooks ***/
2128         if (IS_FLG(FLG_REALM1))
2129         {
2130                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
2131                 body_str = "ËâË¡½ñ";
2132         }
2133
2134         /*** Second realm spellbooks ***/
2135         if (IS_FLG(FLG_REALM2))
2136         {
2137                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
2138                 body_str = "ËâË¡½ñ";
2139         }
2140
2141         /*** First rank spellbooks ***/
2142         if (IS_FLG(FLG_FIRST))
2143         {
2144                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
2145                 body_str = "ËâË¡½ñ";
2146         }
2147
2148         /*** Second rank spellbooks ***/
2149         if (IS_FLG(FLG_SECOND))
2150         {
2151                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
2152                 body_str = "ËâË¡½ñ";
2153         }
2154
2155         /*** Third rank spellbooks ***/
2156         if (IS_FLG(FLG_THIRD))
2157         {
2158                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
2159                 body_str = "ËâË¡½ñ";
2160         }
2161
2162         /*** Fourth rank spellbooks ***/
2163         if (IS_FLG(FLG_FOURTH))
2164         {
2165                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
2166                 body_str = "ËâË¡½ñ";
2167         }
2168
2169         /*** Items ***/
2170         if (IS_FLG(FLG_ITEMS))
2171                 ; /* Nothing to do */
2172         else if (IS_FLG(FLG_WEAPONS))
2173                 body_str = "Éð´ï";
2174         else if (IS_FLG(FLG_ARMORS))
2175                 body_str = "Ëɶñ";
2176         else if (IS_FLG(FLG_MISSILES))
2177                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
2178         else if (IS_FLG(FLG_DEVICES))
2179                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
2180         else if (IS_FLG(FLG_LIGHTS))
2181                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
2182         else if (IS_FLG(FLG_JUNKS))
2183                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
2184         else if (IS_FLG(FLG_SPELLBOOKS))
2185                 body_str = "ËâË¡½ñ";
2186         else if (IS_FLG(FLG_HAFTED))
2187                 body_str = "Æß´ï";
2188         else if (IS_FLG(FLG_SHIELDS))
2189                 body_str = "½â";
2190         else if (IS_FLG(FLG_BOWS))
2191                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
2192         else if (IS_FLG(FLG_RINGS))
2193                 body_str = "»ØÎØ";
2194         else if (IS_FLG(FLG_AMULETS))
2195                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
2196         else if (IS_FLG(FLG_SUITS))
2197                 body_str = "³»";
2198         else if (IS_FLG(FLG_CLOAKS))
2199                 body_str = "¥¯¥í¡¼¥¯";
2200         else if (IS_FLG(FLG_HELMS))
2201                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
2202         else if (IS_FLG(FLG_GLOVES))
2203                 body_str = "äƼê";
2204         else if (IS_FLG(FLG_BOOTS))
2205                 body_str = "¥Ö¡¼¥Ä";
2206         else if (IS_FLG(FLG_FAVORITE))
2207                 body_str = "ÆÀ°ÕÉð´ï";
2208
2209         *buff = '\0';
2210         if (!before_n) 
2211                 strcat(buff, "Á´¤Æ¤Î");
2212         else for (i = 0; i < before_n && before_str[i]; i++)
2213                 strcat(buff, before_str[i]);
2214
2215         strcat(buff, body_str);
2216
2217         if (*str)
2218         {
2219                 if (*str == '^')
2220                 {
2221                         str++;
2222                         top = TRUE;
2223                 }
2224
2225                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
2226                 strncat(buff, str, 80);
2227                 if (top)
2228                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
2229                 else
2230                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
2231         }
2232
2233         if (insc)
2234         {
2235                 strncat(buff, format("¤Ë¡Ö%s¡×", insc), 80);
2236
2237                 if (strstr(insc, "%%all"))
2238                         strcat(buff, "(%%all¤ÏÁ´Ç½ÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2239                 else if (strstr(insc, "%all"))
2240                         strcat(buff, "(%all¤ÏÁ´Ç½ÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2241                 else if (strstr(insc, "%%"))
2242                         strcat(buff, "(%%¤ÏÄɲÃǽÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2243                 else if (strstr(insc, "%"))
2244                         strcat(buff, "(%¤ÏÄɲÃǽÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2245
2246                 strcat(buff, "¤È¹ï¤ó¤Ç");
2247         }
2248         else
2249                 strcat(buff, "¤ò");
2250
2251         if (act & DONT_AUTOPICK)
2252                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
2253         else if (act & DO_AUTODESTROY)
2254                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
2255         else if (act & DO_QUERY_AUTOPICK)
2256                 strcat(buff, "³Îǧ¤Î¸å¤Ë½¦¤¦¡£");
2257         else
2258                 strcat(buff, "½¦¤¦¡£");
2259
2260         if (act & DO_DISPLAY)
2261         {
2262                 if (act & DONT_AUTOPICK)
2263                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2264                 else if (act & DO_AUTODESTROY)
2265                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2266                 else
2267                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2268         }
2269         else
2270                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤¡£");
2271
2272 #else /* JP */
2273
2274         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
2275         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
2276
2277         body_str = "items";
2278
2279         /*** Collecting items ***/
2280         /*** Which can be absorbed into a slot as a bundle ***/
2281         if (IS_FLG(FLG_COLLECTING))
2282                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
2283         
2284         /*** Unidentified ***/
2285         if (IS_FLG(FLG_UNIDENTIFIED))
2286                 before_str[before_n++] = "unidentified";
2287
2288         /*** Identified ***/
2289         if (IS_FLG(FLG_IDENTIFIED))
2290                 before_str[before_n++] = "identified";
2291
2292         /*** *Identified* ***/
2293         if (IS_FLG(FLG_STAR_IDENTIFIED))
2294                 before_str[before_n++] = "fully identified";
2295
2296         /*** Worthless items ***/
2297         if (IS_FLG(FLG_WORTHLESS))
2298         {
2299                 before_str[before_n++] = "worthless";
2300                 which_str[which_n++] = "can not be sold at stores";
2301         }
2302
2303         /*** Artifacto ***/
2304         if (IS_FLG(FLG_ARTIFACT))
2305         {
2306                 before_str[before_n++] = "artifact";
2307         }
2308
2309         /*** Ego ***/
2310         if (IS_FLG(FLG_EGO))
2311         {
2312                 before_str[before_n++] = "ego";
2313         }
2314
2315         /*** Nameless ***/
2316         if (IS_FLG(FLG_NAMELESS))
2317         {
2318                 body_str = "equipment";
2319                 which_str[which_n++] = "is neither ego-item nor artifact";
2320         }
2321
2322         /*** Unaware items ***/
2323         if (IS_FLG(FLG_UNAWARE))
2324         {
2325                 before_str[before_n++] = "unidentified";
2326                 whose_str[whose_n++] = "basic abilities are not known";
2327         }
2328
2329         /*** Dice boosted (weapon of slaying) ***/
2330         if (IS_FLG(FLG_BOOSTED))
2331         {
2332                 body_str = "weapons";
2333                 whose_str[whose_n++] = "damage dice is bigger than normal";
2334         }
2335
2336         /*** Weapons whose dd*ds is more than nn ***/
2337         if (IS_FLG(FLG_MORE_THAN))
2338         {
2339                 static char more_than_desc_str[] =
2340                         "maximum damage from dice is bigger than __";
2341                 body_str = "weapons";
2342                         
2343                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
2344                         "%d", entry->dice);
2345                 whose_str[whose_n++] = more_than_desc_str;
2346         }
2347
2348         /*** Items whose magical bonus is more than nn ***/
2349         if (IS_FLG(FLG_MORE_BONUS))
2350         {
2351                 static char more_bonus_desc_str[] =
2352                         "magical bonus is bigger than (+__)";
2353                         
2354                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
2355                         "%d)", entry->bonus);
2356                 whose_str[whose_n++] = more_bonus_desc_str;
2357         }
2358
2359         /*** Wanted monster's corpse/skeletons ***/
2360         if (IS_FLG(FLG_WANTED))
2361         {
2362                 body_str = "corpse or skeletons";
2363                 which_str[which_n++] = "is wanted at the Hunter's Office";
2364         }
2365
2366         /*** Human corpse/skeletons (for Daemon magic) ***/
2367         if (IS_FLG(FLG_HUMAN))
2368         {
2369                 before_str[before_n++] = "humanoid";
2370                 body_str = "corpse or skeletons";
2371                 which_str[which_n++] = "can be used for Daemon magic";
2372         }
2373
2374         /*** Unique monster's corpse/skeletons/statues ***/
2375         if (IS_FLG(FLG_UNIQUE))
2376         {
2377                 before_str[before_n++] = "unique monster's";
2378                 body_str = "corpse or skeletons";
2379         }
2380
2381         /*** Unreadable spellbooks ***/
2382         if (IS_FLG(FLG_UNREADABLE))
2383         {
2384                 body_str = "spellbooks";
2385                 after_str[after_n++] = "of different realms from yours";
2386         }
2387
2388         /*** First realm spellbooks ***/
2389         if (IS_FLG(FLG_REALM1))
2390         {
2391                 body_str = "spellbooks";
2392                 after_str[after_n++] = "of your first realm";
2393         }
2394
2395         /*** Second realm spellbooks ***/
2396         if (IS_FLG(FLG_REALM2))
2397         {
2398                 body_str = "spellbooks";
2399                 after_str[after_n++] = "of your second realm";
2400         }
2401
2402         /*** First rank spellbooks ***/
2403         if (IS_FLG(FLG_FIRST))
2404         {
2405                 before_str[before_n++] = "first one of four";
2406                 body_str = "spellbooks";
2407         }
2408
2409         /*** Second rank spellbooks ***/
2410         if (IS_FLG(FLG_SECOND))
2411         {
2412                 before_str[before_n++] = "second one of four";
2413                 body_str = "spellbooks";
2414         }
2415
2416         /*** Third rank spellbooks ***/
2417         if (IS_FLG(FLG_THIRD))
2418         {
2419                 before_str[before_n++] = "third one of four";
2420                 body_str = "spellbooks";
2421         }
2422
2423         /*** Fourth rank spellbooks ***/
2424         if (IS_FLG(FLG_FOURTH))
2425         {
2426                 before_str[before_n++] = "fourth one of four";
2427                 body_str = "spellbooks";
2428         }
2429
2430         /*** Items ***/
2431         if (IS_FLG(FLG_ITEMS))
2432                 ; /* Nothing to do */
2433         else if (IS_FLG(FLG_WEAPONS))
2434                 body_str = "weapons";
2435         else if (IS_FLG(FLG_ARMORS))
2436                 body_str = "armors";
2437         else if (IS_FLG(FLG_MISSILES))
2438                 body_str = "shots, arrows or crossbow bolts";
2439         else if (IS_FLG(FLG_DEVICES))
2440                 body_str = "scrolls, wands, staves or rods";
2441         else if (IS_FLG(FLG_LIGHTS))
2442                 body_str = "light sources";
2443         else if (IS_FLG(FLG_JUNKS))
2444                 body_str = "junk such as broken sticks";
2445         else if (IS_FLG(FLG_SPELLBOOKS))
2446                 body_str = "spellbooks";
2447         else if (IS_FLG(FLG_HAFTED))
2448                 body_str = "hafted weapons";
2449         else if (IS_FLG(FLG_SHIELDS))
2450                 body_str = "shields";
2451         else if (IS_FLG(FLG_BOWS))
2452                 body_str = "slings, bows or crossbows";
2453         else if (IS_FLG(FLG_RINGS))
2454                 body_str = "rings";
2455         else if (IS_FLG(FLG_AMULETS))
2456                 body_str = "amulets";
2457         else if (IS_FLG(FLG_SUITS))
2458                 body_str = "body armors";
2459         else if (IS_FLG(FLG_CLOAKS))
2460                 body_str = "cloaks";
2461         else if (IS_FLG(FLG_HELMS))
2462                 body_str = "helms or crowns";
2463         else if (IS_FLG(FLG_GLOVES))
2464                 body_str = "gloves";
2465         else if (IS_FLG(FLG_BOOTS))
2466                 body_str = "boots";
2467         else if (IS_FLG(FLG_FAVORITE))
2468                 body_str = "favorite weapons";
2469
2470         /* Prepare a string for item name */
2471         if (*str)
2472         {
2473                 if (*str == '^')
2474                 {
2475                         str++;
2476                         top = TRUE;
2477                         whose_str[whose_n++] = "name is beginning with \"";
2478                 }
2479                 else
2480                         which_str[which_n++] = "have \"";
2481         }
2482
2483
2484         /* Describe action flag */
2485         if (act & DONT_AUTOPICK)
2486                 strcpy(buff, "Leave on floor ");
2487         else if (act & DO_AUTODESTROY)
2488                 strcpy(buff, "Destroy ");
2489         else if (act & DO_QUERY_AUTOPICK)
2490                 strcpy(buff, "Ask to pick up ");
2491         else
2492                 strcpy(buff, "Pickup ");
2493
2494         /* Auto-insctiption */
2495         if (insc)
2496         {
2497                 strncat(buff, format("and inscribe \"%s\"", insc), 80);
2498
2499                 if (strstr(insc, "%all"))
2500                         strcat(buff, ", replacing %all with code string representing all abilities,");
2501                 else if (strstr(insc, "%"))
2502                         strcat(buff, ", replacing % with code string representing extra random abilities,");
2503
2504                 strcat(buff, " on ");
2505         }
2506
2507         /* Adjective */
2508         if (!before_n) 
2509                 strcat(buff, "all ");
2510         else for (i = 0; i < before_n && before_str[i]; i++)
2511         {
2512                 strcat(buff, before_str[i]);
2513                 strcat(buff, " ");
2514         }
2515
2516         /* Item class */
2517         strcat(buff, body_str);
2518
2519         /* of ... */
2520         for (i = 0; i < after_n && after_str[i]; i++)
2521         {
2522                 strcat(buff, " ");
2523                 strcat(buff, after_str[i]);
2524         }
2525
2526         /* which ... */
2527         for (i = 0; i < whose_n && whose_str[i]; i++)
2528         {
2529                 if (i == 0)
2530                         strcat(buff, " whose ");
2531                 else
2532                         strcat(buff, ", and ");
2533
2534                 strcat(buff, whose_str[i]);
2535         }
2536
2537         /* Item name ; whose name is beginning with "str" */
2538         if (*str && top)
2539         {
2540                 strcat(buff, str);
2541                 strcat(buff, "\"");
2542         }
2543
2544         /* whose ..., and which .... */
2545         if (whose_n && which_n)
2546                 strcat(buff, ", and ");
2547
2548         /* which ... */
2549         for (i = 0; i < which_n && which_str[i]; i++)
2550         {
2551                 if (i == 0)
2552                         strcat(buff, " which ");
2553                 else
2554                         strcat(buff, ", and ");
2555
2556                 strcat(buff, which_str[i]);
2557         }
2558
2559         /* Item name ; which have "str" as part of its name */
2560         if (*str && !top)
2561         {
2562                 strncat(buff, str, 80);
2563                 strcat(buff, "\" as part of its name");
2564         }
2565         strcat(buff, ".");
2566
2567         /* Describe whether it will be displayed on the full map or not */
2568         if (act & DO_DISPLAY)
2569         {
2570                 if (act & DONT_AUTOPICK)
2571                         strcat(buff, "  Display these items when you press the N key in the full 'M'ap.");
2572                 else if (act & DO_AUTODESTROY)
2573                         strcat(buff, "  Display these items when you press the K key in the full 'M'ap.");
2574                 else
2575                         strcat(buff, "  Display these items when you press the M key in the full 'M'ap.");
2576         }
2577         else
2578                 strcat(buff, " Not displayed in the full map.");
2579 #endif /* JP */
2580
2581 }
2582
2583
2584 /*
2585  * Read whole lines of a file to memory
2586  */
2587 static cptr *read_text_lines(cptr filename, bool user)
2588 {
2589         cptr *lines_list = NULL;
2590         FILE *fff;
2591
2592         int lines = 0;
2593         char buf[1024];
2594
2595         if (user)
2596         {
2597                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2598         }
2599         else
2600         {
2601                 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
2602         }
2603         
2604         /* Open the file */
2605         fff = my_fopen(buf, "r");
2606
2607         if (fff)
2608         {
2609                 /* Allocate list of pointers */
2610                 C_MAKE(lines_list, MAX_LINES, cptr);
2611
2612                 /* Parse it */
2613                 while (0 == my_fgets(fff, buf, sizeof(buf)))
2614                 {
2615                         lines_list[lines++] = string_make(buf);
2616                         if (lines >= MAX_LINES - 1) break;
2617                 }
2618                 if (lines == 0)
2619                         lines_list[0] = string_make("");
2620
2621                 my_fclose(fff);
2622         }
2623
2624         if (!fff) return NULL;
2625         return lines_list;
2626 }
2627
2628
2629 static cptr *read_pickpref_text_lines(int *filename_mode_p)
2630 {
2631         char buf[1024];
2632         cptr *lines_list;
2633
2634         *filename_mode_p = PT_WITH_PNAME;
2635         strcpy(buf, pickpref_filename(*filename_mode_p));
2636         lines_list = read_text_lines(buf, TRUE);
2637
2638         if (!lines_list)
2639         {
2640                 *filename_mode_p = PT_DEFAULT;
2641                 strcpy(buf, pickpref_filename(*filename_mode_p));
2642                 lines_list = read_text_lines(buf, TRUE);
2643         }
2644
2645         if (!lines_list)
2646         {
2647                 strcpy(buf, pickpref_filename(*filename_mode_p));
2648                 lines_list = read_text_lines(buf, FALSE);
2649         }
2650
2651         if (!lines_list)
2652         {
2653                 /* Allocate list of pointers */
2654                 C_MAKE(lines_list, MAX_LINES, cptr);
2655                 lines_list[0] = string_make("");
2656         }
2657         return lines_list;
2658 }
2659
2660
2661 /*
2662  * Write whole lines of memory to a file.
2663  */
2664 static bool write_text_lines(cptr filename, cptr *lines_list)
2665 {
2666         FILE *fff;
2667
2668         int lines = 0;
2669         char buf[1024];
2670
2671         /* Build the filename */
2672         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2673         
2674         /* Open the file */
2675         fff = my_fopen(buf, "w");
2676         if (fff)
2677         {
2678                 for (lines = 0; lines_list[lines]; lines++)
2679                         my_fputs(fff, lines_list[lines], 1024);
2680
2681                 my_fclose(fff);
2682         }
2683
2684         if (!fff) return FALSE;
2685         return TRUE;
2686 }
2687
2688
2689 /*
2690  * Free memory of lines_list.
2691  */
2692 static void free_text_lines(cptr *lines_list)
2693 {
2694         int lines;
2695
2696         for (lines = 0; lines_list[lines]; lines++)
2697                 string_free(lines_list[lines]);
2698
2699         /* free list of pointers */
2700         C_FREE((char **)lines_list, MAX_LINES, char *);
2701 }
2702
2703
2704 /*
2705  * Delete or insert string
2706  */
2707 static void toggle_keyword(text_body_type *tb, int flg)
2708 {
2709         int by1, by2, bx1, bx2, y;
2710         bool add = TRUE;
2711         bool fixed = FALSE;
2712
2713         /* Select this line */
2714         if (!tb->mark)
2715         {
2716                 tb->my = tb->cy;
2717                 tb->mx = 0;
2718         }
2719
2720         if (tb->my < tb->cy)
2721         {
2722                 by1 = tb->my;
2723                 by2 = tb->cy;
2724                 bx1 = tb->mx;
2725                 bx2 = tb->cx;
2726         }
2727         else
2728         {
2729                 by2 = tb->my;
2730                 by1 = tb->cy;
2731                 bx2 = tb->mx;
2732                 bx1 = tb->cx;
2733         }
2734
2735         /* String on these lines are not really selected */  
2736         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2737         if (bx2 == 0 && by1 < by2) by2--;
2738
2739
2740         /* Set/Reset flag of each line */
2741         for (y = by1; y <= by2; y++)
2742         {
2743                 autopick_type an_entry, *entry = &an_entry;
2744
2745                 if (!autopick_new_entry(entry, tb->lines_list[y], !fixed)) continue;
2746
2747                 string_free(tb->lines_list[y]);
2748
2749                 if (!fixed)
2750                 {
2751                         /* Add? or Remove? */
2752                         if (!IS_FLG(flg)) add = TRUE;
2753                         else add = FALSE;
2754
2755                         /* No more change */
2756                         fixed = TRUE;
2757                 }
2758
2759                 /* You can use only one noun flag */
2760                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2761                 {
2762                         int i;
2763                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2764                                 REM_FLG(i);
2765                 }
2766                 
2767                 /* You can use only one identify state flag */
2768                 else if (FLG_UNAWARE <= flg && flg <= FLG_STAR_IDENTIFIED)
2769                 {
2770                         int i;
2771                         for (i = FLG_UNAWARE; i <= FLG_STAR_IDENTIFIED; i++)
2772                                 REM_FLG(i);
2773                 }
2774                 
2775                 /* You can use only one flag in artifact/ego/nameless */
2776                 else if (FLG_ARTIFACT <= flg && flg <= FLG_NAMELESS)
2777                 {
2778                         int i;
2779                         for (i = FLG_ARTIFACT; i <= FLG_NAMELESS; i++)
2780                                 REM_FLG(i);
2781                 }
2782                 
2783                 if (add) ADD_FLG(flg);
2784                 else REM_FLG(flg);
2785                 
2786                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2787                 
2788                 /* Now dirty */
2789                 tb->dirty_flags |= DIRTY_ALL;
2790
2791                 /* Text is changed */
2792                 tb->changed = TRUE;
2793         }
2794 }
2795
2796
2797 /*
2798  * Change command letter
2799  */
2800 static void toggle_command_letter(text_body_type *tb, byte flg)
2801 {
2802         autopick_type an_entry, *entry = &an_entry;
2803         int by1, by2, bx1, bx2, y;
2804         bool add = TRUE;
2805         bool fixed = FALSE;
2806
2807         /* Select this line */
2808         if (!tb->mark)
2809         {
2810                 tb->my = tb->cy;
2811                 tb->mx = 0;
2812         }
2813
2814         if (tb->my < tb->cy)
2815         {
2816                 by1 = tb->my;
2817                 by2 = tb->cy;
2818                 bx1 = tb->mx;
2819                 bx2 = tb->cx;
2820         }
2821         else
2822         {
2823                 by2 = tb->my;
2824                 by1 = tb->cy;
2825                 bx2 = tb->mx;
2826                 bx1 = tb->cx;
2827         }
2828
2829         /* String on these lines are not really selected */  
2830         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2831         if (bx2 == 0 && by1 < by2) by2--;
2832
2833         /* Set/Reset flag of each line */
2834         for (y = by1; y <= by2; y++)
2835         {
2836                 int wid = 0;
2837
2838                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2839
2840                 string_free(tb->lines_list[y]);
2841
2842                 if (!fixed)
2843                 {
2844                         /* Add? or Remove? */
2845                         if (!(entry->action & flg)) add = TRUE;
2846                         else add = FALSE;
2847
2848                         /* No more change */
2849                         fixed = TRUE;
2850                 }
2851
2852                 /* Count number of letter (by negative number) */
2853                 if (entry->action & DONT_AUTOPICK) wid--;
2854                 else if (entry->action & DO_AUTODESTROY) wid--;
2855                 else if (entry->action & DO_QUERY_AUTOPICK) wid--;
2856                 if (!(entry->action & DO_DISPLAY)) wid--;
2857
2858                 /* Set/Reset the flag */
2859                 if (flg != DO_DISPLAY)
2860                 {
2861                         entry->action &= ~(DO_AUTOPICK | DONT_AUTOPICK | DO_AUTODESTROY | DO_QUERY_AUTOPICK);
2862                         if (add) entry->action |= flg;
2863                         else entry->action |= DO_AUTOPICK;
2864                 }
2865                 else
2866                 {
2867                         entry->action &= ~(DO_DISPLAY);
2868                         if (add) entry->action |= flg;
2869                 }
2870
2871                 /* Correct cursor location */
2872                 if (tb->cy == y)
2873                 {
2874                         if (entry->action & DONT_AUTOPICK) wid++;
2875                         else if (entry->action & DO_AUTODESTROY) wid++;
2876                         else if (entry->action & DO_QUERY_AUTOPICK) wid++;
2877                         if (!(entry->action & DO_DISPLAY)) wid++;
2878
2879                         if (wid > 0) tb->cx++;
2880                         if (wid < 0 && tb->cx > 0) tb->cx--;
2881                 }
2882                         
2883                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2884                         
2885                 /* Now dirty */
2886                 tb->dirty_flags |= DIRTY_ALL;
2887
2888                 /* Text is changed */
2889                 tb->changed = TRUE;
2890         }
2891 }
2892
2893 /*
2894  * Delete or insert string
2895  */
2896 static void add_keyword(text_body_type *tb, int flg)
2897 {
2898         int by1, by2, bx1, bx2, y;
2899
2900         /* Select this line */
2901         if (!tb->mark)
2902         {
2903                 tb->my = tb->cy;
2904                 tb->mx = 0;
2905         }
2906
2907         if (tb->my < tb->cy)
2908         {
2909                 by1 = tb->my;
2910                 by2 = tb->cy;
2911                 bx1 = tb->mx;
2912                 bx2 = tb->cx;
2913         }
2914         else
2915         {
2916                 by2 = tb->my;
2917                 by1 = tb->cy;
2918                 bx2 = tb->mx;
2919                 bx1 = tb->cx;
2920         }
2921
2922         /* String on these lines are not really selected */  
2923         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2924         if (bx2 == 0 && by1 < by2) by2--;
2925
2926         /* Set/Reset flag of each line */
2927         for (y = by1; y <= by2; y++)
2928         {
2929                 autopick_type an_entry, *entry = &an_entry;
2930
2931                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2932
2933                 /* There is the flag already */
2934                 if (IS_FLG(flg))
2935                 {
2936                         /* Free memory for the entry */
2937                         autopick_free_entry(entry);
2938                         
2939                         continue;
2940                 }
2941                 
2942                 string_free(tb->lines_list[y]);
2943                 
2944                 /* Remove all noun flag */
2945                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2946                 {
2947                         int i;
2948                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2949                                 REM_FLG(i);
2950                 }
2951                 
2952                 ADD_FLG(flg);
2953                 
2954                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2955
2956                 /* Now dirty */
2957                 tb->dirty_flags |= DIRTY_ALL;
2958
2959                 /* Text is changed */
2960                 tb->changed = TRUE;
2961         }
2962 }
2963
2964
2965 /*
2966  * Check if this line is expression or not.
2967  * And update it if it is.
2968  */
2969 static void check_expression_line(text_body_type *tb, int y)
2970 {
2971         cptr s = tb->lines_list[y];
2972
2973         if ((s[0] == '?' && s[1] == ':') ||
2974             (tb->states[y] & LSTAT_BYPASS))
2975         {
2976                 /* Expressions need re-evaluation */
2977                 tb->dirty_flags |= DIRTY_EXPRESSION;
2978         }
2979 }
2980
2981
2982 /*
2983  * Insert return code and split the line
2984  */
2985 static bool insert_return_code(text_body_type *tb)
2986 {
2987         char buf[MAX_LINELEN];
2988         int i, j, k;
2989
2990         for (k = 0; tb->lines_list[k]; k++)
2991                 /* count number of lines */ ;
2992
2993         if (k >= MAX_LINES - 2) return FALSE;
2994         k--;
2995
2996         /* Move down lines */
2997         for (; tb->cy < k; k--)
2998         {
2999                 tb->lines_list[k+1] = tb->lines_list[k];
3000                 tb->states[k+1] = tb->states[k];
3001         }
3002
3003         /* Split current line */
3004         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
3005         {
3006 #ifdef JP
3007                 if (iskanji(tb->lines_list[tb->cy][i]))
3008                         buf[j++] = tb->lines_list[tb->cy][i++];
3009 #endif
3010                 buf[j++] = tb->lines_list[tb->cy][i];
3011         }
3012         buf[j] = '\0';
3013         tb->lines_list[tb->cy+1] = string_make(&tb->lines_list[tb->cy][i]);
3014         string_free(tb->lines_list[tb->cy]);
3015         tb->lines_list[tb->cy] = string_make(buf);
3016
3017         /* Expressions need re-evaluation */
3018         tb->dirty_flags |= DIRTY_EXPRESSION;
3019
3020         /* Text is changed */
3021         tb->changed = TRUE;
3022
3023         return TRUE;
3024 }
3025
3026
3027 /*
3028  * Choose an item and get auto-picker entry from it.
3029  */
3030 static object_type *choose_object(cptr q, cptr s)
3031 {
3032         int item;
3033
3034         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return NULL;
3035
3036         /* Get the item (in the pack) */
3037         if (item >= 0) return &inventory[item];
3038
3039         /* Get the item (on the floor) */
3040         else return &o_list[0 - item];
3041 }
3042
3043
3044 /*
3045  * Choose an item and get auto-picker entry from it.
3046  */
3047 static bool entry_from_choosed_object(autopick_type *entry)
3048 {
3049         object_type *o_ptr;
3050         cptr q, s;
3051
3052         /* Get an item */
3053 #ifdef JP
3054         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
3055         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3056 #else
3057         q = "Enter which item? ";
3058         s = "You have nothing to enter.";
3059 #endif
3060         o_ptr = choose_object(q, s);
3061         if (!o_ptr) return FALSE;
3062
3063         autopick_entry_from_object(entry, o_ptr);
3064         return TRUE;
3065 }
3066
3067
3068 /*
3069  * Choose an item for search
3070  */
3071 static byte get_object_for_search(object_type **o_handle, cptr *search_strp)
3072 {
3073         char buf[MAX_NLEN+20];
3074         object_type *o_ptr;
3075         cptr q, s;
3076
3077         /* Get an item */
3078 #ifdef JP
3079         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò¸¡º÷¤·¤Þ¤¹¤«? ";
3080         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3081 #else
3082         q = "Enter which item? ";
3083         s = "You have nothing to enter.";
3084 #endif
3085         o_ptr = choose_object(q, s);
3086         if (!o_ptr) return 0;
3087
3088         *o_handle = o_ptr;
3089
3090         string_free(*search_strp);
3091         object_desc(buf, *o_handle, FALSE, 3);
3092         *search_strp = string_make(format("<%s>", buf));
3093         return 1;
3094 }
3095
3096
3097 /*
3098  * Prepare for search by destroyed object
3099  */
3100 static byte get_destroyed_object_for_search(object_type **o_handle, cptr *search_strp)
3101 {
3102         char buf[MAX_NLEN+20];
3103
3104         if (!autopick_last_destroyed_object.k_idx) return 0;
3105
3106         *o_handle = &autopick_last_destroyed_object;
3107
3108         string_free(*search_strp);
3109         object_desc(buf, *o_handle, FALSE, 3);
3110         *search_strp = string_make(format("<%s>", buf));
3111         return 1;
3112 }
3113
3114
3115 /*
3116  * Choose an item or string for search
3117  */
3118 static byte get_string_for_search(object_type **o_handle, cptr *search_strp)
3119 {
3120         int pos = 0;
3121
3122         /*
3123          * Text color
3124          * TERM_YELLOW : Overwrite mode
3125          * TERM_WHITE : Insert mode
3126          */
3127         byte color = TERM_YELLOW;
3128         char buf[MAX_NLEN+20];
3129         const size_t len = 80;
3130
3131 #ifdef JP
3132         char prompt[] = "¸¡º÷(^I:»ý¤Áʪ ^L:Ç˲õ¤µ¤ì¤¿Êª): ";
3133 #else
3134         char prompt[] = "Search key(^I:inven ^L:destroyed): ";
3135 #endif
3136         int col = sizeof(prompt) - 1;
3137
3138         if (*search_strp) strcpy(buf, *search_strp);
3139         else buf[0] = '\0';
3140
3141         /* Display prompt */
3142         prt(prompt, 0, 0);
3143
3144
3145         /* Process input */
3146         while (TRUE)
3147         {
3148                 bool back = FALSE;
3149                 int skey;
3150
3151                 /* Display the string */
3152                 Term_erase(col, 0, 255);
3153                 Term_putstr(col, 0, -1, color, buf);
3154
3155                 /* Place cursor */
3156                 Term_gotoxy(col + pos, 0);
3157
3158                 /* Get a special key code */
3159                 skey = inkey_special();
3160
3161                 /* Analyze the key */
3162                 switch (skey)
3163                 {
3164                 case SKEY_LEFT:
3165                 case KTRL('b'):
3166                 {
3167                         int i = 0;
3168
3169                         /* Now on insert mode */
3170                         color = TERM_WHITE;
3171
3172                         /* No move at biggining of line */
3173                         if (0 == pos) break;
3174
3175                         while (TRUE)
3176                         {
3177                                 int next_pos = i + 1;
3178
3179 #ifdef JP
3180                                 if (iskanji(buf[next_pos])) next_pos++;
3181 #endif
3182
3183                                 /* Is there the cursor at next position? */ 
3184                                 if (next_pos >= pos) break;
3185
3186                                 /* Move to next */
3187                                 i = next_pos;
3188                         }
3189
3190                         /* Get previous position */
3191                         pos = i;
3192
3193                         break;
3194                 }
3195
3196                 case SKEY_RIGHT:
3197                 case KTRL('f'):
3198                         /* Now on insert mode */
3199                         color = TERM_WHITE;
3200
3201                         /* No move at end of line */
3202                         if ('\0' == buf[pos]) break;
3203
3204 #ifdef JP
3205                         /* Move right */
3206                         if (iskanji(buf[pos])) pos += 2;
3207                         else pos++;
3208 #else
3209                         pos++;
3210 #endif
3211
3212                         break;
3213
3214                 case ESCAPE:
3215                         return 0;
3216
3217                 case KTRL('r'):
3218                         back = TRUE;
3219                         /* Fall through */
3220
3221                 case '\n':
3222                 case '\r':
3223                 case KTRL('s'):
3224                         if (!pos && *o_handle) return (back ? -1 : 1);
3225                         string_free(*search_strp);
3226                         *search_strp = string_make(buf);
3227                         *o_handle = NULL;
3228                         return (back ? -1 : 1);
3229
3230                 case KTRL('i'):
3231                         /* Get an item */
3232                         return get_object_for_search(o_handle, search_strp);
3233
3234                 case KTRL('l'):
3235                         /* Prepare string for destroyed object if there is one. */
3236                         if (get_destroyed_object_for_search(o_handle, search_strp))
3237                                 return 1;
3238                         break;
3239
3240                 case '\010':
3241                         /* Backspace */
3242
3243                         /* Now on insert mode */
3244                         color = TERM_WHITE;
3245
3246                         /* No move at biggining of line */
3247                         if (!pos) break;
3248
3249                         /* Go left 1 unit */
3250                         pos--;
3251
3252                         /* Fall through to 'Delete key' */
3253
3254                 case 0x7F:
3255                 case KTRL('d'):
3256                         /* Delete key */
3257                 {
3258                         int dst, src;
3259
3260                         /* Now on insert mode */
3261                         color = TERM_WHITE;
3262
3263                         /* Position of next character */
3264                         src = pos + 1;
3265
3266 #ifdef JP
3267                         /* Next character is one more byte away */
3268                         if (iskanji(src)) src++;
3269 #endif
3270
3271                         dst = pos;
3272
3273                         /* Move characters at src to dst */
3274                         while ('\0' != (buf[dst++] = buf[src++]))
3275                                 /* loop */;
3276
3277                         break;
3278                 }
3279
3280                 default:
3281                 {
3282                         /* Insert a character */
3283
3284                         char tmp[100];
3285                         char c;
3286
3287                         /* Ignore special keys */
3288                         if (skey & SKEY_MASK) break;
3289
3290                         /* Get a character code */
3291                         c = (char)skey;
3292
3293                         if (color == TERM_YELLOW)
3294                         {
3295                                 /* Overwrite default string */
3296                                 buf[0] = '\0';
3297
3298                                 /* Go to insert mode */
3299                                 color = TERM_WHITE;
3300                         }
3301
3302                         /* Save right part of string */
3303                         strcpy(tmp, buf + pos);
3304 #ifdef JP
3305                         if (iskanji(c))
3306                         {
3307                                 char next;
3308
3309                                 /* Bypass macro processing */
3310                                 inkey_base = TRUE;
3311                                 next = inkey();
3312
3313                                 if (pos + 1 < len)
3314                                 {
3315                                         buf[pos++] = c;
3316                                         buf[pos++] = next;
3317                                 }
3318                                 else
3319                                 {
3320                                         bell();
3321                                 }
3322                         }
3323                         else
3324 #endif
3325                         {
3326 #ifdef JP
3327                                 if (pos < len && (isprint(c) || iskana(c)))
3328 #else
3329                                 if (pos < len && isprint(c))
3330 #endif
3331                                 {
3332                                         buf[pos++] = c;
3333                                 }
3334                                 else
3335                                 {
3336                                         bell();
3337                                 }
3338                         }
3339
3340                         /* Terminate */
3341                         buf[pos] = '\0';
3342
3343                         /* Write back the left part of string */
3344                         my_strcat(buf, tmp, len + 1);
3345
3346                         break;
3347                 } /* default: */
3348
3349                 }
3350
3351         } /* while (TRUE) */
3352 }
3353
3354
3355 /*
3356  * Search next line matches for o_ptr
3357  */
3358 static bool search_for_object(text_body_type *tb, object_type *o_ptr, bool forward)
3359 {
3360         int i;
3361         autopick_type an_entry, *entry = &an_entry;
3362         char o_name[MAX_NLEN];
3363
3364         object_desc(o_name, o_ptr, FALSE, 3);
3365
3366         /* Force to be lower case string */
3367         for (i = 0; o_name[i]; i++)
3368         {
3369 #ifdef JP
3370                 if (iskanji(o_name[i]))
3371                         i++;
3372                 else
3373 #endif
3374                 if (isupper(o_name[i]))
3375                         o_name[i] = tolower(o_name[i]);
3376         }
3377         
3378         i = tb->cy;
3379
3380         while (1)
3381         {
3382                 if (forward)
3383                 {
3384                         if (!tb->lines_list[++i]) break;
3385                 }
3386                 else
3387                 {
3388                         if (--i < 0) break;
3389                 }
3390
3391                 /* Ignore bypassed lines */
3392                 if (tb->states[i] & LSTAT_BYPASS) continue;
3393
3394                 if (!autopick_new_entry(entry, tb->lines_list[i], FALSE)) continue;
3395
3396                 if (is_autopick_aux(o_ptr, entry, o_name))
3397                 {
3398                         tb->cx = 0;
3399                         tb->cy = i;
3400                         return TRUE;
3401                 }
3402         }
3403
3404         return FALSE;
3405 }
3406
3407
3408 /*
3409  * Search next line matches to the string
3410  */
3411 static bool search_for_string(text_body_type *tb, cptr search_str, bool forward)
3412 {
3413         int i = tb->cy;
3414
3415         while (1)
3416         {
3417                 cptr pos;
3418
3419                 if (forward)
3420                 {
3421                         if (!tb->lines_list[++i]) break;
3422                 }
3423                 else
3424                 {
3425                         if (--i < 0) break;
3426                 }
3427
3428                 /* Ignore bypassed lines */
3429                 if (tb->states[i] & LSTAT_BYPASS) continue;
3430
3431 #ifdef JP
3432                 pos = strstr_j(tb->lines_list[i], search_str);
3433 #else
3434                 pos = strstr(tb->lines_list[i], search_str);
3435 #endif
3436                 if (pos)
3437                 {
3438                         tb->cx = (int)(pos - tb->lines_list[i]);
3439                         tb->cy = i;
3440                         return TRUE;
3441                 }
3442         }
3443
3444         return FALSE;
3445 }
3446
3447
3448
3449
3450 /*
3451  * Editor command id's
3452  */
3453 #define EC_QUIT                1 
3454 #define EC_SAVEQUIT            2     
3455 #define EC_REVERT              3 
3456 #define EC_HELP                4 
3457 #define EC_RETURN              5        
3458 #define EC_LEFT                6 
3459 #define EC_DOWN                7 
3460 #define EC_UP                  8 
3461 #define EC_RIGHT               9 
3462 #define EC_BOL                 10
3463 #define EC_EOL                 11
3464 #define EC_PGUP                12
3465 #define EC_PGDOWN              13
3466 #define EC_TOP                 14
3467 #define EC_BOTTOM              15
3468 #define EC_CUT                 16
3469 #define EC_COPY                17
3470 #define EC_PASTE               18
3471 #define EC_BLOCK               19
3472 #define EC_KILL_LINE           20
3473 #define EC_DELETE_CHAR         21
3474 #define EC_BACKSPACE           22
3475 #define EC_SEARCH_STR          23
3476 #define EC_SEARCH_FORW         24
3477 #define EC_SEARCH_BACK         25
3478 #define EC_SEARCH_OBJ          26
3479 #define EC_SEARCH_DESTROYED    27
3480 #define EC_INSERT_OBJECT       28
3481 #define EC_INSERT_DESTROYED    29
3482 #define EC_INSERT_BLOCK        30
3483 #define EC_INSERT_MACRO        31
3484 #define EC_INSERT_KEYMAP       32
3485 #define EC_CL_AUTOPICK         33
3486 #define EC_CL_DESTROY          34
3487 #define EC_CL_LEAVE            35
3488 #define EC_CL_QUERY            36
3489 #define EC_CL_NO_DISP          37
3490 #define EC_OK_COLLECTING       38
3491 #define EC_IK_UNAWARE          39
3492 #define EC_IK_UNIDENTIFIED     40
3493 #define EC_IK_IDENTIFIED       41
3494 #define EC_IK_STAR_IDENTIFIED  42
3495 #define EC_OK_BOOSTED          43
3496 #define EC_OK_MORE_THAN        44
3497 #define EC_OK_MORE_BONUS       45
3498 #define EC_OK_WORTHLESS        46
3499 #define EC_OK_ARTIFACT         47
3500 #define EC_OK_EGO              48
3501 #define EC_OK_NAMELESS         49
3502 #define EC_OK_WANTED           50
3503 #define EC_OK_UNIQUE           51
3504 #define EC_OK_HUMAN            52
3505 #define EC_OK_UNREADABLE       53
3506 #define EC_OK_REALM1           54
3507 #define EC_OK_REALM2           55
3508 #define EC_OK_FIRST            56
3509 #define EC_OK_SECOND           57
3510 #define EC_OK_THIRD            58
3511 #define EC_OK_FOURTH           59
3512 #define EC_KK_WEAPONS          60
3513 #define EC_KK_FAVORITE         61
3514 #define EC_KK_ARMORS           62
3515 #define EC_KK_MISSILES         63
3516 #define EC_KK_DEVICES          64
3517 #define EC_KK_LIGHTS           65
3518 #define EC_KK_JUNKS            66
3519 #define EC_KK_SPELLBOOKS       67
3520 #define EC_KK_SHIELDS          68
3521 #define EC_KK_BOWS             69
3522 #define EC_KK_RINGS            70
3523 #define EC_KK_AMULETS          71
3524 #define EC_KK_SUITS            72
3525 #define EC_KK_CLOAKS           73
3526 #define EC_KK_HELMS            74
3527 #define EC_KK_GLOVES           75
3528 #define EC_KK_BOOTS            76
3529
3530
3531 /* Manu names */
3532 #ifdef JP
3533
3534 #define MN_QUIT "¥»¡¼¥Ö̵¤·¤Ç½ªÎ»" 
3535 #define MN_SAVEQUIT "¥»¡¼¥Ö¤·¤Æ½ªÎ»" 
3536 #define MN_REVERT "Á´¤Æ¤ÎÊѹ¹¤òÇË´þ" 
3537 #define MN_HELP "¥Ø¥ë¥×" 
3538
3539 #define MN_MOVE "¥«¡¼¥½¥ë°ÜÆ°" 
3540 #define MN_LEFT "º¸" 
3541 #define MN_DOWN "²¼" 
3542 #define MN_UP "¾å" 
3543 #define MN_RIGHT "±¦" 
3544 #define MN_BOL "¹Ô¤ÎÀèƬ" 
3545 #define MN_EOL "¹Ô¤Î½ªÃ¼" 
3546 #define MN_PGUP "¾å¤Î¥Ú¡¼¥¸" 
3547 #define MN_PGDOWN "²¼¤Î¥Ú¡¼¥¸" 
3548 #define MN_TOP "1¹ÔÌܤذÜÆ°" 
3549 #define MN_BOTTOM "ºÇ²¼¹Ô¤Ø°ÜÆ°" 
3550
3551 #define MN_EDIT "ÊÔ½¸" 
3552 #define MN_CUT "¥«¥Ã¥È" 
3553 #define MN_COPY "¥³¥Ô¡¼" 
3554 #define MN_PASTE "¥Ú¡¼¥¹¥È" 
3555 #define MN_BLOCK "ÁªÂòÈϰϤλØÄê" 
3556 #define MN_KILL_LINE "¹Ô¤Î»Ä¤ê¤òºï½ü" 
3557 #define MN_DELETE_CHAR "1ʸ»úºï½ü" 
3558 #define MN_BACKSPACE "¥Ð¥Ã¥¯¥¹¥Ú¡¼¥¹" 
3559 #define MN_RETURN "²þ¹Ô" 
3560 #define MN_RETURN "²þ¹Ô" 
3561
3562 #define MN_SEARCH "¸¡º÷" 
3563 #define MN_SEARCH_STR "ʸ»úÎó¤Ç¸¡º÷" 
3564 #define MN_SEARCH_FORW "Á°Êý¤ØºÆ¸¡º÷" 
3565 #define MN_SEARCH_BACK "¸åÊý¤ØºÆ¸¡º÷" 
3566 #define MN_SEARCH_OBJ "¥¢¥¤¥Æ¥à¤òÁªÂò¤·¤Æ¸¡º÷" 
3567 #define MN_SEARCH_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Ç¸¡º÷" 
3568
3569 #define MN_INSERT "¿§¡¹ÁÞÆþ" 
3570 #define MN_INSERT_OBJECT "ÁªÂò¤·¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3571 #define MN_INSERT_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3572 #define MN_INSERT_BLOCK "¾ò·ïʬ´ô¥Ö¥í¥Ã¥¯¤ÎÎã¤òÁÞÆþ" 
3573 #define MN_INSERT_MACRO "¥Þ¥¯¥íÄêµÁ¤òÁÞÆþ" 
3574 #define MN_INSERT_KEYMAP "¥­¡¼¥Þ¥Ã¥×ÄêµÁ¤òÁÞÆþ" 
3575
3576 #define MN_COMMAND_LETTER "½¦¤¤/Ç˲õ/ÊüÃÖ¤ÎÁªÂò" 
3577 #define MN_CL_AUTOPICK "¡Ö ¡× (¼«Æ°½¦¤¤)" 
3578 #define MN_CL_DESTROY "¡Ö!¡× (¼«Æ°Ç˲õ)" 
3579 #define MN_CL_LEAVE "¡Ö~¡× (ÊüÃÖ)" 
3580 #define MN_CL_QUERY "¡Ö;¡× (³Îǧ¤·¤Æ½¦¤¦)" 
3581 #define MN_CL_NO_DISP "¡Ö(¡× (¥Þ¥Ã¥×¥³¥Þ¥ó¥É¤Çɽ¼¨¤·¤Ê¤¤)" 
3582
3583 #define MN_ADJECTIVE_GEN "·ÁÍÆ»ì(°ìÈÌ)¤ÎÁªÂò" 
3584
3585 #define MN_ADJECTIVE_SPECIAL "·ÁÍÆ»ì(Æüì)¤ÎÁªÂò" 
3586 #define MN_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦 (Éð´ï)" 
3587 #define MN_MORE_THAN "¥À¥¤¥¹ÌÜ # °Ê¾å¤Î (Éð´ï)" 
3588 #define MN_MORE_BONUS "½¤ÀµÃÍ # °Ê¾å¤Î (»ØÎØÅù)" 
3589 #define MN_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È (ÁõÈ÷)" 
3590 #define MN_EGO "¥¨¥´ (ÁõÈ÷)" 
3591 #define MN_NAMELESS "̵ÌäΠ(ÁõÈ÷)" 
3592 #define MN_WANTED "¾Þ¶â¼ó¤Î (»àÂÎ)" 
3593 #define MN_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î (»àÂÎ)" 
3594 #define MN_HUMAN "¿Í´Ö¤Î (»àÂÎ)" 
3595 #define MN_UNREADABLE "Æɤá¤Ê¤¤ (ËâË¡½ñ)" 
3596 #define MN_REALM1 "Âè°ìÎΰè¤Î (ËâË¡½ñ)" 
3597 #define MN_REALM2 "ÂèÆóÎΰè¤Î (ËâË¡½ñ)" 
3598 #define MN_FIRST "1ºýÌܤΠ(ËâË¡½ñ)" 
3599 #define MN_SECOND "2ºýÌܤΠ(ËâË¡½ñ)" 
3600 #define MN_THIRD "3ºýÌܤΠ(ËâË¡½ñ)" 
3601 #define MN_FOURTH "4ºýÌܤΠ(ËâË¡½ñ)" 
3602
3603 #define MN_NOUN "̾»ì¤ÎÁªÂò" 
3604
3605 #else
3606
3607 #define MN_QUIT "Quit without save" 
3608 #define MN_SAVEQUIT "Save & Quit" 
3609 #define MN_REVERT "Revert all changes" 
3610 #define MN_HELP "Help" 
3611
3612 #define MN_MOVE "Move cursor" 
3613 #define MN_LEFT "Left" 
3614 #define MN_DOWN "Down" 
3615 #define MN_UP "Up" 
3616 #define MN_RIGHT "Right" 
3617 #define MN_BOL "Beggining of line" 
3618 #define MN_EOL "End of line" 
3619 #define MN_PGUP "Page up" 
3620 #define MN_PGDOWN "Page down" 
3621 #define MN_TOP "Top" 
3622 #define MN_BOTTOM "Bottom" 
3623
3624 #define MN_EDIT "Edit" 
3625 #define MN_CUT "Cut" 
3626 #define MN_COPY "Copy" 
3627 #define MN_PASTE "Paste" 
3628 #define MN_BLOCK "Select block" 
3629 #define MN_KILL_LINE "Kill rest of line" 
3630 #define MN_DELETE_CHAR "Delete character" 
3631 #define MN_BACKSPACE "Backspace" 
3632 #define MN_RETURN "Return" 
3633 #define MN_RETURN "Return" 
3634
3635 #define MN_SEARCH "Search" 
3636 #define MN_SEARCH_STR "Search by string" 
3637 #define MN_SEARCH_FORW "Search forward" 
3638 #define MN_SEARCH_BACK "Search backward" 
3639 #define MN_SEARCH_OBJ "Search by inventory object" 
3640 #define MN_SEARCH_DESTROYED "Search by destroyed object" 
3641
3642 #define MN_INSERT "Insert..." 
3643 #define MN_INSERT_OBJECT "Insert name of choosen object" 
3644 #define MN_INSERT_DESTROYED "Insert name of destroyed object" 
3645 #define MN_INSERT_BLOCK "Insert conditional block" 
3646 #define MN_INSERT_MACRO "Insert a macro definition" 
3647 #define MN_INSERT_KEYMAP "Insert a keymap definition" 
3648
3649 #define MN_COMMAND_LETTER "Command letter" 
3650 #define MN_CL_AUTOPICK "' ' (Auto pick)" 
3651 #define MN_CL_DESTROY "'!' (Auto destroy)" 
3652 #define MN_CL_LEAVE "'~' (Leave it on the floor)" 
3653 #define MN_CL_QUERY "';' (Query to pick up)" 
3654 #define MN_CL_NO_DISP "'(' (No display on the large map)" 
3655
3656 #define MN_ADJECTIVE_GEN "Adjective (general)" 
3657
3658 #define MN_ADJECTIVE_SPECIAL "Adjective (special)" 
3659 #define MN_BOOSTED "dice boosted (weapons)" 
3660 #define MN_MORE_THAN "more than # dice (weapons)" 
3661 #define MN_MORE_BONUS "more bonus than # (rings etc.)" 
3662 #define MN_ARTIFACT "artifact (equipments)" 
3663 #define MN_EGO "ego (equipments)" 
3664 #define MN_NAMELESS "nameless (equipments)" 
3665 #define MN_WANTED "wanted (corpse)" 
3666 #define MN_UNIQUE "unique (corpse)" 
3667 #define MN_HUMAN "human (corpse)" 
3668 #define MN_UNREADABLE "unreadable (spellbooks)" 
3669 #define MN_REALM1 "realm1 (spellbooks)" 
3670 #define MN_REALM2 "realm2 (spellbooks)" 
3671 #define MN_FIRST "first (spellbooks)" 
3672 #define MN_SECOND "second (spellbooks)" 
3673 #define MN_THIRD "third (spellbooks)" 
3674 #define MN_FOURTH "fourth (spellbooks)" 
3675
3676 #define MN_NOUN "Keywords (noun)" 
3677
3678 #endif
3679
3680
3681 typedef struct {
3682         cptr name;
3683         int level;
3684         int key;
3685         int com_id;
3686 } command_menu_type;
3687
3688
3689 command_menu_type menu_data[] =
3690 {
3691         {MN_HELP, 0, -1, EC_HELP},
3692         {MN_QUIT, 0, KTRL('q'), EC_QUIT}, 
3693         {MN_SAVEQUIT, 0, KTRL('w'), EC_SAVEQUIT}, 
3694         {MN_REVERT, 0, KTRL('z'), EC_REVERT},
3695
3696         {MN_EDIT, 0, -1, -1},
3697         {MN_CUT, 1, KTRL('x'), EC_CUT},
3698         {MN_COPY, 1, KTRL('c'), EC_COPY},
3699         {MN_PASTE, 1, KTRL('v'), EC_PASTE},
3700         {MN_BLOCK, 1, KTRL('g'), EC_BLOCK},
3701         {MN_KILL_LINE, 1, KTRL('k'), EC_KILL_LINE},
3702         {MN_DELETE_CHAR, 1, KTRL('d'), EC_DELETE_CHAR},
3703         {MN_BACKSPACE, 1, KTRL('h'), EC_BACKSPACE},
3704         {MN_RETURN, 1, KTRL('j'), EC_RETURN},
3705         {MN_RETURN, 1, KTRL('m'), EC_RETURN},
3706
3707         {MN_SEARCH, 0, -1, -1},
3708         {MN_SEARCH_STR, 1, KTRL('s'), EC_SEARCH_STR},
3709         {MN_SEARCH_FORW, 1, -1, EC_SEARCH_FORW},
3710         {MN_SEARCH_BACK, 1, KTRL('r'), EC_SEARCH_BACK},
3711         {MN_SEARCH_OBJ, 1, KTRL('y'), EC_SEARCH_OBJ},
3712         {MN_SEARCH_DESTROYED, 1, -1, EC_SEARCH_DESTROYED},
3713
3714         {MN_MOVE, 0, -1, -1},
3715         {MN_LEFT, 1, KTRL('b'), EC_LEFT},
3716         {MN_DOWN, 1, KTRL('n'), EC_DOWN},
3717         {MN_UP, 1, KTRL('p'), EC_UP},
3718         {MN_RIGHT, 1, KTRL('f'), EC_RIGHT},
3719         {MN_BOL, 1, KTRL('a'), EC_BOL},
3720         {MN_EOL, 1, KTRL('e'), EC_EOL},
3721         {MN_PGUP, 1, KTRL('o'), EC_PGUP},
3722         {MN_PGDOWN, 1, KTRL('l'), EC_PGDOWN},
3723         {MN_TOP, 1, KTRL('t'), EC_TOP},
3724         {MN_BOTTOM, 1, KTRL('u'), EC_BOTTOM},
3725
3726         {MN_INSERT, 0, -1, -1},
3727         {MN_INSERT_OBJECT, 1, KTRL('i'), EC_INSERT_OBJECT},
3728         {MN_INSERT_DESTROYED, 1, -1, EC_INSERT_DESTROYED},
3729         {MN_INSERT_BLOCK, 1, -1, EC_INSERT_BLOCK},
3730         {MN_INSERT_MACRO, 1, -1, EC_INSERT_MACRO},
3731         {MN_INSERT_KEYMAP, 1, -1, EC_INSERT_KEYMAP},
3732
3733         {MN_ADJECTIVE_GEN, 0, -1, -1},
3734         {KEY_UNAWARE, 1, -1, EC_IK_UNAWARE},
3735         {KEY_UNIDENTIFIED, 1, -1, EC_IK_UNIDENTIFIED},
3736         {KEY_IDENTIFIED, 1, -1, EC_IK_IDENTIFIED},
3737         {KEY_STAR_IDENTIFIED, 1, -1, EC_IK_STAR_IDENTIFIED},
3738         {KEY_COLLECTING, 1, -1, EC_OK_COLLECTING},
3739         {KEY_WORTHLESS, 1, -1, EC_OK_WORTHLESS},
3740
3741         {MN_ADJECTIVE_SPECIAL, 0, -1, -1},
3742         {MN_BOOSTED, 1, -1, EC_OK_BOOSTED},
3743         {MN_MORE_THAN, 1, -1, EC_OK_MORE_THAN},
3744         {MN_MORE_BONUS, 1, -1, EC_OK_MORE_BONUS},
3745         {MN_ARTIFACT, 1, -1, EC_OK_ARTIFACT},
3746         {MN_EGO, 1, -1, EC_OK_EGO},
3747         {MN_NAMELESS, 1, -1, EC_OK_NAMELESS},
3748         {MN_WANTED, 1, -1, EC_OK_WANTED},
3749         {MN_UNIQUE, 1, -1, EC_OK_UNIQUE},
3750         {MN_HUMAN, 1, -1, EC_OK_HUMAN},
3751         {MN_UNREADABLE, 1, -1, EC_OK_UNREADABLE},
3752         {MN_REALM1, 1, -1, EC_OK_REALM1},
3753         {MN_REALM2, 1, -1, EC_OK_REALM2},
3754         {MN_FIRST, 1, -1, EC_OK_FIRST},
3755         {MN_SECOND, 1, -1, EC_OK_SECOND},
3756         {MN_THIRD, 1, -1, EC_OK_THIRD},
3757         {MN_FOURTH, 1, -1, EC_OK_FOURTH},
3758
3759         {MN_NOUN, 0, -1, -1},
3760         {KEY_WEAPONS, 1, -1, EC_KK_WEAPONS},
3761         {KEY_FAVORITE, 1, -1, EC_KK_FAVORITE},
3762         {KEY_ARMORS, 1, -1, EC_KK_ARMORS},
3763         {KEY_MISSILES, 1, -1, EC_KK_MISSILES},
3764         {KEY_DEVICES, 1, -1, EC_KK_DEVICES},
3765         {KEY_LIGHTS, 1, -1, EC_KK_LIGHTS},
3766         {KEY_JUNKS, 1, -1, EC_KK_JUNKS},
3767         {KEY_SPELLBOOKS, 1, -1, EC_KK_SPELLBOOKS},
3768         {KEY_SHIELDS, 1, -1, EC_KK_SHIELDS},
3769         {KEY_BOWS, 1, -1, EC_KK_BOWS},
3770         {KEY_RINGS, 1, -1, EC_KK_RINGS},
3771         {KEY_AMULETS, 1, -1, EC_KK_AMULETS},
3772         {KEY_SUITS, 1, -1, EC_KK_SUITS},
3773         {KEY_CLOAKS, 1, -1, EC_KK_CLOAKS},
3774         {KEY_HELMS, 1, -1, EC_KK_HELMS},
3775         {KEY_GLOVES, 1, -1, EC_KK_GLOVES},
3776         {KEY_BOOTS, 1, -1, EC_KK_BOOTS},
3777
3778         {MN_COMMAND_LETTER, 0, -1, -1},
3779         {MN_CL_AUTOPICK, 1, -1, EC_CL_AUTOPICK},
3780         {MN_CL_DESTROY, 1, -1, EC_CL_DESTROY},
3781         {MN_CL_LEAVE, 1, -1, EC_CL_LEAVE},
3782         {MN_CL_QUERY, 1, -1, EC_CL_QUERY},
3783         {MN_CL_NO_DISP, 1, -1, EC_CL_NO_DISP},
3784
3785         {MN_DELETE_CHAR, -1, 0x7F, EC_DELETE_CHAR},
3786
3787         {NULL, -1, -1, 0}
3788 };
3789
3790
3791 /*
3792  * Find a command by 'key'.
3793  */
3794 static int get_com_id(char key)
3795 {
3796         int i;
3797
3798         for (i = 0; menu_data[i].name; i++)
3799         {
3800                 if (menu_data[i].key == key)
3801                 {
3802                         return menu_data[i].com_id;
3803                 }
3804         }
3805
3806         return 0;
3807 }
3808
3809
3810 /*
3811  * Display the menu, and get a command 
3812  */
3813 static int do_command_menu(int level, int start)
3814 {
3815         int i;
3816         int max_len = 0;
3817         int max_menu_wid;
3818         int col0 = 5 + level*7;
3819         int row0 = 1 + level*3;
3820         byte menu_key = 0;
3821         int menu_id_list[26];
3822         bool redraw = TRUE;
3823         char linestr[MAX_LINELEN];
3824
3825         /* Get max length */
3826         menu_key = 0;
3827         for (i = start; menu_data[i].level >= level; i++)
3828         {
3829                 int len;
3830
3831                 /* Ignore lower level sub menus */
3832                 if (menu_data[i].level > level) continue;
3833
3834                 len = strlen(menu_data[i].name);
3835                 if (len > max_len) max_len = len;
3836
3837                 menu_id_list[menu_key] = i;
3838                 menu_key++;
3839         }
3840
3841         while (menu_key < 26)
3842         {
3843                 menu_id_list[menu_key] = -1;
3844                 menu_key++;
3845         }
3846
3847         /* Extra space for displaying menu key and command key */
3848         max_menu_wid = max_len + 3 + 3;
3849
3850         /* Prepare box line */
3851         linestr[0] = '\0';
3852         strcat(linestr, "+");
3853         for (i = 0; i < max_menu_wid + 2; i++)
3854         {
3855                 strcat(linestr, "-");
3856         }
3857         strcat(linestr, "+");
3858
3859         while (1)
3860         {
3861                 int com_id;
3862                 char key;
3863                 int menu_id;
3864
3865                 if (redraw)
3866                 {
3867                         int row1 = row0 + 1;
3868
3869                         /* Draw top line */
3870                         Term_putstr(col0, row0, -1, TERM_WHITE, linestr);
3871
3872                         /* Draw menu items */
3873                         menu_key = 0;
3874                         for (i = start; menu_data[i].level >= level; i++)
3875                         {
3876                                 char com_key_str[3];
3877                                 cptr str;
3878
3879                                 /* Ignore lower level sub menus */
3880                                 if (menu_data[i].level > level) continue;
3881
3882                                 if (menu_data[i].com_id == -1)
3883                                 {
3884 #ifdef JP
3885                                         strcpy(com_key_str, "¢§");
3886 #else
3887                                         strcpy(com_key_str, ">");
3888 #endif
3889                                 }
3890                                 else if (menu_data[i].key != -1)
3891                                 {
3892                                         com_key_str[0] = '^';
3893                                         com_key_str[1] = menu_data[i].key + '@';
3894                                         com_key_str[2] = '\0';
3895                                 }
3896                                 else
3897                                 {
3898                                         com_key_str[0] = '\0';
3899                                 }
3900
3901                                 str = format("| %c) %-*s %2s | ", menu_key + 'a', max_len, menu_data[i].name, com_key_str);
3902
3903                                 Term_putstr(col0, row1++, -1, TERM_WHITE, str);
3904
3905                                 menu_key++;
3906                         }
3907
3908                         /* Draw bottom line */
3909                         Term_putstr(col0, row1, -1, TERM_WHITE, linestr);
3910
3911                         /* The menu was shown */
3912                         redraw = FALSE;
3913                 }
3914 #ifdef JP
3915                 prt(format("(a-%c) ¥³¥Þ¥ó¥É:", menu_key + 'a' - 1), 0, 0);
3916 #else
3917                 prt(format("(a-%c) Command:", menu_key + 'a' - 1), 0, 0);
3918 #endif
3919                 key = inkey();
3920
3921                 if (key == ESCAPE) return 0;
3922
3923                 if ('a' <= key && key <= 'z')
3924                 {
3925                         menu_id = menu_id_list[key - 'a'];
3926
3927                         if (menu_id >= 0)
3928                         {
3929                                 com_id = menu_data[menu_id].com_id;
3930
3931                                 if (com_id == -1)
3932                                 {
3933                                         com_id = do_command_menu(level + 1, menu_id + 1);
3934
3935                                         if (com_id) return com_id;
3936                                         else redraw = TRUE;
3937                                 }
3938                                 else if (com_id)
3939                                 {
3940                                         return com_id;
3941                                 }
3942                         }
3943                 }
3944
3945                 else
3946                 {
3947                         com_id = get_com_id(key);
3948                         if (com_id) return com_id;
3949                         else continue;
3950                 }
3951         }
3952 }
3953
3954
3955 static chain_str_type *new_chain_str(cptr str)
3956 {
3957         chain_str_type *chain;
3958
3959         size_t len = strlen(str);
3960
3961         chain = (chain_str_type *)ralloc(sizeof(chain_str_type) + len * sizeof(char));
3962
3963         strcpy(chain->s, str);
3964         chain->next = NULL;
3965
3966         return chain;
3967 }
3968
3969
3970 static void kill_yank_chain(text_body_type *tb)
3971 {
3972         chain_str_type *chain = tb->yank;
3973         tb->yank = NULL;
3974         tb->yank_eol = TRUE;
3975
3976         while (chain)
3977         {
3978                 chain_str_type *next = chain->next;
3979                 size_t len = strlen(chain->s);
3980
3981                 rnfree(chain, sizeof(chain_str_type) + len * sizeof(char));
3982
3983                 chain = next;
3984         }
3985 }
3986
3987
3988 static void add_str_to_yank(text_body_type *tb, cptr str)
3989 {
3990         chain_str_type *chain;
3991
3992         tb->yank_eol = FALSE;
3993
3994         if (NULL == tb->yank)
3995         {
3996                 tb->yank = new_chain_str(str);
3997                 return;
3998         }
3999
4000         chain = tb->yank;
4001
4002         while (1)
4003         {
4004                 if (!chain->next)
4005                 {
4006                         chain->next = new_chain_str(str);
4007                         return;
4008                 }
4009
4010                 /* Go to next */
4011                 chain = chain->next;
4012         }
4013 }
4014
4015
4016 #define DESCRIPT_HGT 3
4017
4018 /*
4019  * Draw text
4020  */
4021 static void draw_text_editor(text_body_type *tb)
4022 {
4023         int i;
4024         int by1 = -1, bx1 = -1, by2 = -1, bx2 = -1;
4025
4026         /* Get size */
4027         Term_get_size(&tb->wid, &tb->hgt);
4028
4029         /*
4030          * Top line (-1), description line (-3), separator (-1)
4031          *  == -5
4032          */
4033         tb->hgt -= 2 + DESCRIPT_HGT;
4034
4035 #ifdef JP
4036         /* Don't let cursor at second byte of kanji */
4037         for (i = 0; tb->lines_list[tb->cy][i]; i++)
4038                 if (iskanji(tb->lines_list[tb->cy][i]))
4039                 {
4040                         i++;
4041                         if (i == tb->cx)
4042                         {
4043                                 tb->cx--;
4044                                 break;
4045                         }
4046                 }
4047 #endif
4048
4049         /* Scroll if necessary */
4050         if (tb->cy < tb->upper || tb->upper + tb->hgt <= tb->cy)
4051                 tb->upper = tb->cy - (tb->hgt)/2;
4052         if (tb->upper < 0)
4053                 tb->upper = 0;
4054         if ((tb->cx < tb->left + 10 && tb->left > 0) || tb->left + tb->wid - 5 <= tb->cx)
4055                 tb->left = tb->cx - (tb->wid)*2/3;
4056         if (tb->left < 0)
4057                 tb->left = 0;
4058
4059         /* Redraw whole window after resize */
4060         if (tb->old_wid != tb->wid || tb->old_hgt != tb->hgt)
4061                 tb->dirty_flags |= DIRTY_SCREEN;
4062
4063         /* Redraw all text after scroll */
4064         else if (tb->old_upper != tb->upper || tb->old_left != tb->left)
4065                 tb->dirty_flags |= DIRTY_ALL;
4066
4067
4068         if (tb->dirty_flags & DIRTY_SCREEN)
4069         {
4070                 tb->dirty_flags |= (DIRTY_ALL | DIRTY_MODE);
4071
4072                 /* Clear screen */
4073                 Term_clear();
4074         }
4075
4076         /* Redraw mode line */
4077         if (tb->dirty_flags & DIRTY_MODE)
4078         {
4079                 char buf[MAX_LINELEN];
4080
4081                 int sepa_length = tb->wid;
4082
4083                 /* Separator */
4084                 for (i = 0; i < sepa_length; i++)
4085                         buf[i] = '-';
4086                 buf[i] = '\0';
4087
4088                 Term_putstr(0, tb->hgt + 1, sepa_length, TERM_WHITE, buf);
4089         }
4090
4091         if (tb->dirty_flags & DIRTY_EXPRESSION)
4092         {
4093                 int y;
4094                 byte state = 0;
4095
4096                 for (y = 0; tb->lines_list[y]; y++)
4097                 {
4098                         char f;
4099                         cptr v;
4100                         cptr s = tb->lines_list[y];
4101
4102                         /* Update this line's state */
4103                         tb->states[y] = state;
4104
4105                         if (*s++ != '?') continue;
4106                         if (*s++ != ':') continue;
4107
4108                         /* Lines below this line are auto-registered */
4109                         if (streq(s, "$AUTOREGISTER"))
4110                                 state |= LSTAT_AUTOREGISTER;
4111
4112                         /* Parse the expr */
4113                         v = process_pref_file_expr(&s, &f);
4114
4115                         /* Set flag */
4116                         if (streq(v, "0")) state |= LSTAT_BYPASS;
4117                         else state &= ~LSTAT_BYPASS;
4118
4119                         /* Re-update this line's state */
4120                         tb->states[y] = state | LSTAT_EXPRESSION;
4121                 }
4122
4123                 tb->dirty_flags |= DIRTY_ALL;
4124         }
4125
4126         if (tb->mark)
4127         {
4128                 int tmp_cx = tb->cx;
4129                 int len = strlen(tb->lines_list[tb->cy]);
4130
4131                 /* Correct cursor location */
4132                 if (tb->cx > len) tmp_cx = len;
4133
4134                 tb->dirty_flags |= DIRTY_ALL;
4135
4136                 if (tb->my < tb->cy ||
4137                     (tb->my == tb->cy && tb->mx < tmp_cx))
4138                 {
4139                         by1 = tb->my;
4140                         bx1 = tb->mx;
4141                         by2 = tb->cy;
4142                         bx2 = tmp_cx;
4143                 }
4144                 else
4145                 {
4146                         by2 = tb->my;
4147                         bx2 = tb->mx;
4148                         by1 = tb->cy;
4149                         bx1 = tmp_cx;
4150                 }
4151         }
4152
4153         /* Dump up to tb->hgt lines of messages */
4154         for (i = 0; i < tb->hgt; i++)
4155         {
4156                 int j;
4157                 int leftcol = 0;
4158                 cptr msg;
4159                 byte color;
4160                 int y = tb->upper+i;
4161
4162                 /* clean or dirty? */
4163                 if (!(tb->dirty_flags & DIRTY_ALL) && (tb->dirty_line != y))
4164                         continue;
4165
4166                 msg = tb->lines_list[y];
4167                 if (!msg) break;
4168
4169                 /* Apply horizontal scroll */
4170                 for (j = 0; *msg; msg++, j++)
4171                 {
4172                         if (j == tb->left) break;
4173 #ifdef JP
4174                         if (j > tb->left)
4175                         {
4176                                 leftcol = 1;
4177                                 break;
4178                         }
4179                         if (iskanji(*msg))
4180                         {
4181                                 msg++;
4182                                 j++;
4183                         }
4184 #endif
4185                 }
4186
4187                 /* Erase line */
4188                 Term_erase(0, i + 1, tb->wid);
4189
4190                 if (tb->states[y] & LSTAT_AUTOREGISTER)
4191                 {
4192                         /* Warning color -- These lines will be deleted later */
4193                         color = TERM_L_RED;
4194                 }
4195                 else
4196                 {
4197                         /* Bypassed line will be displayed by darker color */
4198                         if (tb->states[y] & LSTAT_BYPASS) color = TERM_SLATE;
4199                         else color = TERM_WHITE;
4200                 }
4201
4202                 if (!tb->mark)
4203                 {
4204                         /* Dump the messages, bottom to top */
4205                         Term_putstr(leftcol, i + 1, tb->wid - 1, color, msg);
4206                 }
4207
4208                 else
4209                 {
4210                         int x0 = leftcol + tb->left;
4211
4212                         int sx0 = 0;
4213                         int sx1 = 0;
4214
4215                         if (by1 <= y && y < by2) sx1 = strlen(msg);
4216                         if (y == by1) sx0 = bx1;
4217                         if (y == by2) sx1 = bx2;
4218
4219                         Term_gotoxy(leftcol, i + 1);
4220                         if (x0 < sx0) Term_addstr(sx0 - x0, color, msg);
4221                         if (x0 < sx1) Term_addstr(sx1 - sx0, TERM_YELLOW, msg + (sx0 - x0));
4222                         Term_addstr(-1, color, msg + (sx1 - x0));
4223                 }
4224         }
4225
4226         for (; i < tb->hgt; i++)
4227         {
4228                 /* Erase line */
4229                 Term_erase(0, i + 1, tb->wid);
4230         }
4231
4232         /* Display information when updated */
4233         if (tb->old_cy != tb->cy || (tb->dirty_flags & (DIRTY_ALL | DIRTY_NOT_FOUND | DIRTY_NO_SEARCH)) || tb->dirty_line == tb->cy)
4234         {
4235                 autopick_type an_entry, *entry = &an_entry;
4236
4237                 /* Clear information line */
4238                 for (i = 0; i < DESCRIPT_HGT; i++)
4239                 {
4240                         /* Erase line */
4241                         Term_erase(0, tb->hgt + 2 + i, tb->wid);
4242                 }
4243
4244                 /* Display information */
4245                 if (tb->dirty_flags & DIRTY_NOT_FOUND)
4246                 {
4247 #ifdef JP
4248                         prt(format("¥Ñ¥¿¡¼¥ó¤¬¸«¤Ä¤«¤ê¤Þ¤»¤ó: %s", tb->search_str), tb->hgt + 1 + 1, 0);
4249 #else
4250                         prt(format("Pattern not found: %s", tb->search_str), tb->hgt + 1 + 1, 0);
4251 #endif
4252                 }
4253                 else if (tb->dirty_flags & DIRTY_NO_SEARCH)
4254                 {
4255 #ifdef JP
4256                         prt("¸¡º÷Ãæ¤Î¥Ñ¥¿¡¼¥ó¤¬¤¢¤ê¤Þ¤»¤ó('/'¤Ç¸¡º÷)¡£", tb->hgt + 1 + 1, 0);
4257 #else
4258                         prt("No pattern to search. (Press '/' to search.)", tb->hgt +1 + 1, 0);
4259 #endif
4260                 }
4261                 else if (tb->lines_list[tb->cy][0] == '#')
4262                 {
4263 #ifdef JP
4264                         prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", tb->hgt +1 + 1, 0);
4265 #else
4266                         prt("This line is a comment.", tb->hgt +1 + 1, 0);
4267 #endif
4268                 }
4269                 else if (tb->lines_list[tb->cy][1] == ':')
4270                 {
4271                         cptr str = NULL;
4272
4273                         switch(tb->lines_list[tb->cy][0])
4274                         {
4275                         case '?':
4276 #ifdef JP
4277                                 str = "¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£";
4278 #else
4279                                 str = "This line is a Conditional Expression.";
4280 #endif
4281
4282                                 break;
4283                         case 'A':
4284 #ifdef JP
4285                                 str = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£";
4286 #else
4287                                 str = "This line defines a Macro action.";
4288 #endif
4289                                 break;
4290                         case 'P':
4291 #ifdef JP
4292                                 str = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£";
4293 #else
4294                                 str = "This line defines a Macro trigger key.";
4295 #endif
4296                                 break;
4297                         case 'C':
4298 #ifdef JP
4299                                 str = "¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£";
4300 #else
4301                                 str = "This line defines a Keymap.";
4302 #endif
4303                                 break;
4304                         }
4305
4306                         /* Draw the first line */
4307                         if (str) prt(str, tb->hgt +1 + 1, 0);
4308
4309                         str = NULL;
4310
4311                         switch(tb->lines_list[tb->cy][0])
4312                         {
4313                         case '?':
4314                                 if (tb->states[tb->cy] & LSTAT_BYPASS)
4315                                 {
4316 #ifdef JP
4317                                         str = "¸½ºß¤Î¼°¤ÎÃͤϡֵ¶(=0)¡×¤Ç¤¹¡£";
4318 #else
4319                                         str = "The expression is 'False'(=0) currently.";
4320 #endif
4321                                 }
4322                                 else
4323                                 {
4324 #ifdef JP
4325                                         str = "¸½ºß¤Î¼°¤ÎÃͤϡֿ¿(=1)¡×¤Ç¤¹¡£";
4326 #else
4327                                         str = "The expression is 'True'(=1) currently.";
4328 #endif
4329                                 }
4330                                 break;
4331
4332                         default:
4333                                 if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4334                                 {
4335 #ifdef JP
4336                                         str = "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£";
4337 #else
4338                                         str = "This line will be delete later.";
4339 #endif
4340                                 }
4341
4342                                 else if (tb->states[tb->cy] & LSTAT_BYPASS)
4343                                 {
4344 #ifdef JP
4345                                         str = "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£";
4346 #else
4347                                         str = "This line is bypassed currently.";
4348 #endif
4349                                 }
4350                                 break;
4351                         }
4352
4353                         /* Draw the second line */
4354                         if (str) prt(str, tb->hgt +1 + 2, 0);
4355                 }
4356
4357                 /* Get description of an autopicker preference line */
4358                 else if (autopick_new_entry(entry, tb->lines_list[tb->cy], FALSE))
4359                 {
4360                         char buf[MAX_LINELEN];
4361                         char temp[MAX_LINELEN];
4362                         cptr t;
4363
4364                         describe_autopick(buf, entry);
4365
4366                         if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4367                         {
4368 #ifdef JP
4369                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£");
4370 #else
4371                                 strcat(buf, "  This line will be delete later.");
4372 #endif
4373                         }
4374
4375                         if (tb->states[tb->cy] & LSTAT_BYPASS)
4376                         {
4377 #ifdef JP
4378                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£");
4379 #else
4380                                 strcat(buf, "  This line is bypassed currently.");
4381 #endif
4382                         }
4383
4384                         roff_to_buf(buf, 81, temp, sizeof(temp));
4385                         t = temp;
4386                         for (i = 0; i < 3; i++)
4387                         {
4388                                 if(t[0] == 0)
4389                                         break; 
4390                                 else
4391                                 {
4392                                         prt(t, tb->hgt +1 + 1 + i, 0);
4393                                         t += strlen(t) + 1;
4394                                 }
4395                         }
4396                         autopick_free_entry(entry);
4397                 }
4398         }
4399 }
4400
4401
4402 /*
4403  * Kill segment of a line
4404  */
4405 static void kill_line_segment(text_body_type *tb, int y, int x0, int x1, bool whole)
4406 {
4407         char buf[MAX_LINELEN];
4408         cptr s = tb->lines_list[y];
4409         char *d = buf;
4410         int x;
4411
4412         /* No segment? */
4413         if (x0 == x1) return;
4414
4415         /* Kill whole line? */
4416         if (whole && x0 == 0 && s[x1] == '\0' && tb->lines_list[y+1])
4417         {
4418                 int i;
4419
4420                 string_free(tb->lines_list[y]);
4421
4422                 /* Shift lines up */
4423                 for (i = y; tb->lines_list[i+1]; i++)
4424                         tb->lines_list[i] = tb->lines_list[i+1];
4425                 tb->lines_list[i] = NULL;
4426
4427                 /* Expressions need re-evaluation */
4428                 tb->dirty_flags |= DIRTY_EXPRESSION;
4429
4430                 return;
4431         }
4432
4433         /* Before the segment */
4434         for (x = 0; x < x0; x++)
4435                 *(d++) = s[x];
4436
4437         /* After the segment */
4438         for (x = x1; s[x]; x++)
4439                 *(d++) = s[x];
4440
4441         *d = '\0';
4442
4443         /* Replace */
4444         string_free(tb->lines_list[y]);
4445         tb->lines_list[y] = string_make(buf);
4446
4447         /* Expressions may need re-evaluation */
4448         check_expression_line(tb, y);
4449
4450         /* Text is changed */
4451         tb->changed = TRUE;
4452 }
4453
4454
4455 /*
4456  * Get a trigger key and insert ASCII string for the trigger
4457  */
4458 static bool insert_macro_line(text_body_type *tb)
4459 {
4460         char tmp[1024];
4461         char buf[1024];
4462         int i, n = 0;
4463
4464         /* Flush */
4465         flush();
4466
4467         /* Do not process macros */
4468         inkey_base = TRUE;
4469
4470         /* First key */
4471         i = inkey();
4472
4473         /* Read the pattern */
4474         while (i)
4475         {
4476                 /* Save the key */
4477                 buf[n++] = i;
4478
4479                 /* Do not process macros */
4480                 inkey_base = TRUE;
4481
4482                 /* Do not wait for keys */
4483                 inkey_scan = TRUE;
4484
4485                 /* Attempt to read a key */
4486                 i = inkey();
4487         }
4488
4489         /* Terminate */
4490         buf[n] = '\0';
4491
4492         /* Flush */
4493         flush();
4494
4495         /* Convert the trigger */
4496         ascii_to_text(tmp, buf);
4497
4498         /* Null */
4499         if(!tmp[0]) return FALSE;
4500
4501         tb->cx = 0;
4502
4503         /* Insert preference string */
4504         insert_return_code(tb);
4505         string_free(tb->lines_list[tb->cy]);
4506         tb->lines_list[tb->cy] = string_make(format("P:%s", tmp));
4507
4508         /* Acquire action */
4509         i = macro_find_exact(buf);
4510
4511         if (i == -1)
4512         {
4513                 /* Nothing defined */
4514                 tmp[0] = '\0';
4515         }
4516         else
4517         {
4518                 /* Analyze the current action */
4519                 ascii_to_text(tmp, macro__act[i]);
4520         }
4521
4522         /* Insert blank action preference line */
4523         insert_return_code(tb);
4524         string_free(tb->lines_list[tb->cy]);
4525         tb->lines_list[tb->cy] = string_make(format("A:%s", tmp));
4526
4527         return TRUE;
4528 }
4529
4530
4531 /*
4532  * Get a command key and insert ASCII string for the key
4533  */
4534 static bool insert_keymap_line(text_body_type *tb)
4535 {
4536         char tmp[1024];
4537         char buf[2];
4538         int mode;
4539         cptr act;
4540
4541         /* Roguelike */
4542         if (rogue_like_commands)
4543         {
4544                 mode = KEYMAP_MODE_ROGUE;
4545         }
4546
4547         /* Original */
4548         else
4549         {
4550                 mode = KEYMAP_MODE_ORIG;
4551         }
4552
4553         /* Flush */
4554         flush();
4555
4556         /* Get a key */
4557         buf[0] = inkey();
4558         buf[1] = '\0';
4559
4560         /* Flush */
4561         flush();
4562
4563         /* Convert the trigger */
4564         ascii_to_text(tmp, buf);
4565
4566         /* Null */
4567         if(!tmp[0]) return FALSE;
4568
4569         tb->cx = 0;
4570
4571         /* Insert preference string */
4572         insert_return_code(tb);
4573         string_free(tb->lines_list[tb->cy]);
4574         tb->lines_list[tb->cy] = string_make(format("C:%d:%s", mode, tmp));
4575
4576         /* Look up the keymap */
4577         act = keymap_act[mode][(byte)(buf[0])];
4578
4579         /* Insert blank action preference line */
4580         insert_return_code(tb);
4581         string_free(tb->lines_list[tb->cy]);
4582         tb->lines_list[tb->cy] = string_make(format("A:%s", act));
4583
4584         return TRUE;
4585 }
4586
4587
4588 /*
4589  * Execute a single editor command
4590  */
4591 static bool do_editor_command(text_body_type *tb, int com_id)
4592 {
4593         switch(com_id)
4594         {
4595         case EC_QUIT:
4596                 if (tb->changed)
4597                 {
4598 #ifdef JP
4599                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¤«¤é½ªÎ»¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4600 #else
4601                         if (!get_check("Discard all changes and quit. Are you sure? ")) break;
4602 #endif
4603                 }
4604                 return QUIT_WITHOUT_SAVE;
4605
4606         case EC_SAVEQUIT:
4607                 return QUIT_AND_SAVE;
4608
4609         case EC_REVERT:
4610                 /* Revert to original */
4611 #ifdef JP
4612                 if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4613 #else
4614                 if (!get_check("Discard all changes and revert to original file. Are you sure? ")) break;
4615 #endif
4616
4617                 free_text_lines(tb->lines_list);
4618                 tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
4619                 tb->dirty_flags |= DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
4620                 tb->cx = tb->cy = 0;
4621                 tb->mark = 0;
4622
4623                 /* Text is not changed */
4624                 tb->changed = FALSE;
4625                 break;
4626
4627         case EC_HELP:
4628                 /* Peruse the main help file */
4629 #ifdef JP
4630                 (void)show_file(TRUE, "jeditor.txt", NULL, 0, 0);
4631 #else
4632                 (void)show_file(TRUE, "editor.txt", NULL, 0, 0);
4633 #endif
4634                 /* Redraw all */
4635                 tb->dirty_flags |= DIRTY_SCREEN;
4636
4637                 break;
4638
4639         case EC_RETURN:
4640                 /* Split a line or insert end of line */
4641
4642                 /* Ignore selection */
4643                 if (tb->mark)
4644                 {
4645                         tb->mark = 0;
4646
4647                         /* Now dirty */
4648                         tb->dirty_flags |= DIRTY_ALL;
4649                 }
4650
4651                 insert_return_code(tb);
4652                 tb->cy++;
4653                 tb->cx = 0;
4654
4655                 /* Now dirty */
4656                 tb->dirty_flags |= DIRTY_ALL;
4657                 break;
4658
4659         case EC_LEFT:
4660                 /* Back */
4661                 if (0 < tb->cx)
4662                 {
4663                         int len;
4664
4665                         tb->cx--;
4666                         len = strlen(tb->lines_list[tb->cy]);
4667                         if (len < tb->cx) tb->cx = len;
4668                 }
4669                 else if (tb->cy > 0)
4670                 {
4671                         tb->cy--;
4672                         tb->cx = strlen(tb->lines_list[tb->cy]);
4673                 }
4674                 break;
4675
4676         case EC_DOWN:
4677                 /* Next line */
4678                 if (tb->lines_list[tb->cy + 1]) tb->cy++;
4679                 break;
4680
4681         case EC_UP:
4682                 /* Previous line */
4683                 if (tb->cy > 0) tb->cy--;
4684                 break;
4685
4686         case EC_RIGHT:
4687         {
4688                 /* Forward */
4689
4690                 int len;
4691 #ifdef JP
4692                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
4693 #endif
4694                 tb->cx++;
4695                 len = strlen(tb->lines_list[tb->cy]);
4696                 if (len < tb->cx)
4697                 {
4698                         if (tb->lines_list[tb->cy + 1])
4699                         {
4700                                 tb->cy++;
4701                                 tb->cx = 0;
4702                         }
4703                         else
4704                                 tb->cx = len;
4705                 }
4706                 break;
4707         }
4708
4709         case EC_BOL:
4710                 /* Beginning of line */
4711                 tb->cx = 0;
4712                 break;
4713
4714         case EC_EOL:
4715                 /* End of line */
4716                 tb->cx = strlen(tb->lines_list[tb->cy]);
4717                 break;
4718
4719         case EC_PGUP:
4720                 while (0 < tb->cy && tb->upper <= tb->cy)
4721                         tb->cy--;
4722                 while (0 < tb->upper && tb->cy + 1 < tb->upper + tb->hgt)
4723                         tb->upper--;
4724                 break;
4725
4726         case EC_PGDOWN:
4727                 /* Page down */
4728                 while (tb->cy < tb->upper + tb->hgt && tb->lines_list[tb->cy + 1])
4729                         tb->cy++;
4730                 tb->upper = tb->cy;
4731                 break;
4732
4733         case EC_TOP:
4734                 tb->cy = 0;
4735                 break;
4736
4737         case EC_BOTTOM:
4738                 while (tb->lines_list[tb->cy + 1])
4739                         tb->cy++;
4740                 break;
4741
4742         case EC_CUT:
4743         {       
4744                 int by1, bx1, by2, bx2;
4745                 int y;
4746
4747                 /* Copy the text first */
4748                 do_editor_command(tb, EC_COPY);
4749
4750                 if (tb->my < tb->cy ||
4751                     (tb->my == tb->cy && tb->mx < tb->cx))
4752                 {
4753                         by1 = tb->my;
4754                         bx1 = tb->mx;
4755                         by2 = tb->cy;
4756                         bx2 = tb->cx;
4757                 }
4758                 else
4759                 {
4760                         by2 = tb->my;
4761                         bx2 = tb->mx;
4762                         by1 = tb->cy;
4763                         bx1 = tb->cx;
4764                 }
4765
4766                 /* Kill lines in reverse order */
4767                 for (y = by2; y >= by1; y--)
4768                 {
4769                         int x0 = 0;
4770                         int x1 = strlen(tb->lines_list[y]);
4771
4772                         if (y == by1) x0 = bx1;
4773                         if (y == by2) x1 = bx2;
4774
4775                         kill_line_segment(tb, y, x0, x1, TRUE);
4776                 }
4777
4778                 /* Correct cursor position */
4779                 tb->cy = by1;
4780                 tb->cx = bx1;
4781
4782                 /* Disable selection */
4783                 tb->mark = 0;
4784
4785                 /* Now dirty */
4786                 tb->dirty_flags |= DIRTY_ALL;
4787
4788                 break;
4789         }
4790
4791         case EC_COPY:
4792         {       
4793                 int by1, bx1, by2, bx2;
4794                 int y;
4795                 int len = strlen(tb->lines_list[tb->cy]);
4796
4797                 /* Correct cursor location */
4798                 if (tb->cx > len) tb->cx = len;
4799
4800                 /* Use single line? */
4801                 if (!tb->mark)
4802                 {
4803                         tb->my = tb->cy;
4804                         tb->mx = 0;
4805                         if (tb->lines_list[tb->cy + 1])
4806                         {
4807                                 /* Select a single line */
4808                                 tb->cx = 0;
4809                                 tb->cy++;
4810                         }
4811                         else
4812                         {
4813                                 /* Select bottom line */
4814                                 tb->cx = len;
4815                         }
4816                 }
4817
4818                 if (tb->my < tb->cy ||
4819                     (tb->my == tb->cy && tb->mx < tb->cx))
4820                 {
4821                         by1 = tb->my;
4822                         bx1 = tb->mx;
4823                         by2 = tb->cy;
4824                         bx2 = tb->cx;
4825                 }
4826                 else
4827                 {
4828                         by2 = tb->my;
4829                         bx2 = tb->mx;
4830                         by1 = tb->cy;
4831                         bx1 = tb->cx;
4832                 }
4833
4834                 /* Kill old yank buffer */
4835                 kill_yank_chain(tb);
4836
4837                 /* Copy string to yank buffer */
4838                 for (y = by1; y <= by2; y++)
4839                 {
4840                         int i;
4841                         char buf[MAX_LINELEN];
4842
4843                         int x0 = 0;
4844                         int x1 = strlen(tb->lines_list[y]);
4845
4846                         if (y == by1) x0 = bx1;
4847                         if (y == by2) x1 = bx2;
4848
4849                         for (i = 0; i < x1 - x0; i++)
4850                         {
4851                                 buf[i] = tb->lines_list[y][x0 + i];
4852                         }
4853                         buf[i] = '\0';
4854
4855                         add_str_to_yank(tb, buf);
4856                 }
4857
4858                 /* Disable selection */
4859                 tb->mark = 0;
4860
4861                 /* Now dirty */
4862                 tb->dirty_flags |= DIRTY_ALL;
4863                 break;
4864         }
4865
4866         case EC_PASTE:
4867         {
4868                 /* Paste killed text */
4869
4870                 chain_str_type *chain = tb->yank;
4871                 int len = strlen(tb->lines_list[tb->cy]);
4872
4873                 /* Nothing to do? */
4874                 if (!chain) break;
4875
4876                 /* Correct cursor location */
4877                 if (tb->cx > len) tb->cx = len;
4878
4879                 /* Ignore selection */
4880                 if (tb->mark)
4881                 {
4882                         tb->mark = 0;
4883
4884                         /* Now dirty */
4885                         tb->dirty_flags |= DIRTY_ALL;
4886                 }
4887
4888                 /* Paste text */
4889                 while (chain)
4890                 {
4891                         cptr yank_str = chain->s;
4892
4893                         char buf[MAX_LINELEN];
4894                         int i;
4895                         char rest[MAX_LINELEN], *rest_ptr = rest;
4896
4897                         /* Save preceding string */
4898                         for(i = 0; i < tb->cx; i++)
4899                                 buf[i] = tb->lines_list[tb->cy][i];
4900
4901                         strcpy(rest, &(tb->lines_list[tb->cy][i]));
4902
4903                         /* Paste yank buffer */
4904                         while (*yank_str && i < MAX_LINELEN-1)
4905                         {
4906                                 buf[i++] = *yank_str++;
4907                         }
4908
4909                         /* Terminate */
4910                         buf[i] = '\0';
4911
4912                         chain = chain->next;
4913
4914                         if (chain || tb->yank_eol)
4915                         {
4916                                 /* There is an end of line between chain nodes */
4917
4918                                 insert_return_code(tb);
4919
4920                                 /* Replace this line with new one */
4921                                 string_free(tb->lines_list[tb->cy]);
4922                                 tb->lines_list[tb->cy] = string_make(buf);
4923
4924                                 /* Move to next line */
4925                                 tb->cx = 0;
4926                                 tb->cy++;
4927
4928                                 continue;
4929                         }
4930
4931                         /* Final line doesn't have end of line */
4932
4933                         tb->cx = strlen(buf);
4934
4935                         /* Rest of original line */
4936                         while (*rest_ptr && i < MAX_LINELEN-1)
4937                         {
4938                                 buf[i++] = *rest_ptr++;
4939                         }
4940
4941                         /* Terminate */
4942                         buf[i] = '\0';
4943
4944                         /* Replace this line with new one */
4945                         string_free(tb->lines_list[tb->cy]);
4946                         tb->lines_list[tb->cy] = string_make(buf);
4947
4948                         /* Finish */
4949                         break;
4950                 }
4951
4952                 /* Now dirty */
4953                 tb->dirty_flags |= DIRTY_ALL;
4954
4955                 /* Expressions need re-evaluation */
4956                 tb->dirty_flags |= DIRTY_EXPRESSION;
4957
4958                 /* Text is changed */
4959                 tb->changed = TRUE;
4960
4961                 break;
4962         }
4963
4964         case EC_BLOCK:
4965                 if (tb->mark)
4966                 {
4967                         /* Disable the selection */
4968                         tb->mark = 0;
4969
4970                         /* Redraw text */
4971                         tb->dirty_flags |= DIRTY_ALL;
4972                 }
4973                 else
4974                 {
4975                         tb->mark = MARK_MARK;
4976
4977                         /* Repeating this command swaps cursor position */
4978                         if (com_id == tb->old_com_id)
4979                         {
4980                                 int tmp;
4981
4982                                 tmp = tb->cy;
4983                                 tb->cy = tb->my;
4984                                 tb->my = tmp;
4985                                 tmp = tb->cx;
4986                                 tb->cx = tb->mx;
4987                                 tb->mx = tmp;
4988
4989                                 /* Redraw text */
4990                                 tb->dirty_flags |= DIRTY_ALL;
4991                         }
4992                         else
4993                         {
4994                                 int len = strlen(tb->lines_list[tb->cy]);
4995
4996                                 /* Mark the point 1 */
4997                                 tb->my = tb->cy;
4998                                 tb->mx = tb->cx;
4999
5000                                 /* Correct cursor location */
5001                                 if (tb->cx > len) tb->mx = len;
5002                         }
5003                 }
5004                 break;
5005
5006         case EC_KILL_LINE:
5007         {
5008                 /* Kill rest of line */
5009
5010                 int len = strlen(tb->lines_list[tb->cy]);
5011
5012                 /* Correct cursor location */
5013                 if (tb->cx > len) tb->cx = len;
5014
5015                 /* Ignore selection */
5016                 if (tb->mark)
5017                 {
5018                         tb->mark = 0;
5019
5020                         /* Now dirty */
5021                         tb->dirty_flags |= DIRTY_ALL;
5022                 }
5023
5024                 /* Append only if this command is repeated. */
5025                 if (tb->old_com_id != com_id)
5026                 {
5027                         kill_yank_chain(tb);
5028                         tb->yank = NULL;
5029                 }
5030
5031                 /* Really deleted some text */
5032                 if (tb->cx < len)
5033                 {
5034                         /* Add deleted string to yank buffer */
5035                         add_str_to_yank(tb, &(tb->lines_list[tb->cy][tb->cx]));
5036
5037                         kill_line_segment(tb, tb->cy, tb->cx, len, FALSE);
5038
5039                         /* Now dirty */
5040                         tb->dirty_line = tb->cy;
5041
5042                         /* Leave end of line character */
5043                         break;
5044                 }
5045
5046                 /* Cut the end of line character only */
5047                 if (tb->yank_eol) add_str_to_yank(tb, "");
5048
5049                 /* Cut end of line */
5050                 tb->yank_eol = TRUE;
5051
5052                 do_editor_command(tb, EC_DELETE_CHAR);
5053                 break;
5054         }
5055
5056         case EC_DELETE_CHAR:
5057                 /* DELETE == go forward + BACK SPACE */
5058
5059                 /* Ignore selection */
5060                 if (tb->mark)
5061                 {
5062                         tb->mark = 0;
5063
5064                         /* Now dirty */
5065                         tb->dirty_flags |= DIRTY_ALL;
5066                 }
5067
5068 #ifdef JP
5069                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
5070 #endif
5071                 tb->cx++;
5072
5073                 do_editor_command(tb, EC_BACKSPACE);
5074                 break;
5075
5076         case EC_BACKSPACE:
5077         {
5078                 /* BACK SPACE */
5079
5080                 int len, i, j, k;
5081                 char buf[MAX_LINELEN];
5082
5083                 /* Ignore selection */
5084                 if (tb->mark)
5085                 {
5086                         tb->mark = 0;
5087
5088                         /* Now dirty */
5089                         tb->dirty_flags |= DIRTY_ALL;
5090                 }
5091
5092                 len = strlen(tb->lines_list[tb->cy]);
5093                 if (len < tb->cx)
5094                 {
5095                         if (tb->lines_list[tb->cy + 1])
5096                         {
5097                                 tb->cy++;
5098                                 tb->cx = 0;
5099                         }
5100                         else
5101                         {
5102                                 tb->cx = len;
5103                                 break;
5104                         }
5105                 }
5106
5107                 if (tb->cx == 0)
5108                 {
5109                         /* delete a return code and union two lines */
5110                         if (tb->cy == 0) break;
5111                         tb->cx = strlen(tb->lines_list[tb->cy-1]);
5112                         strcpy(buf, tb->lines_list[tb->cy-1]);
5113                         strcat(buf, tb->lines_list[tb->cy]);
5114                         string_free(tb->lines_list[tb->cy-1]);
5115                         string_free(tb->lines_list[tb->cy]);
5116                         tb->lines_list[tb->cy-1] = string_make(buf);
5117
5118                         for (i = tb->cy; tb->lines_list[i+1]; i++)
5119                                 tb->lines_list[i] = tb->lines_list[i+1];
5120
5121                         tb->lines_list[i] = NULL;
5122                         tb->cy--;
5123
5124                         /* Now dirty */
5125                         tb->dirty_flags |= DIRTY_ALL;
5126
5127                         /* Expressions need re-evaluation */
5128                         tb->dirty_flags |= DIRTY_EXPRESSION;
5129
5130                         /* Text is changed */
5131                         tb->changed = TRUE;
5132
5133                         break;
5134                 }
5135
5136                 for (i = j = k = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
5137                 {
5138                         k = j;
5139 #ifdef JP
5140                         if (iskanji(tb->lines_list[tb->cy][i]))
5141                                 buf[j++] = tb->lines_list[tb->cy][i++];
5142 #endif
5143                         buf[j++] = tb->lines_list[tb->cy][i];
5144                 }
5145                 while (j > k)
5146                 {
5147                         tb->cx--;
5148                         j--;
5149                 }
5150                 for (; tb->lines_list[tb->cy][i]; i++)
5151                         buf[j++] = tb->lines_list[tb->cy][i];
5152                 buf[j] = '\0';
5153                 string_free(tb->lines_list[tb->cy]);
5154                 tb->lines_list[tb->cy] = string_make(buf);
5155
5156                 /* Now dirty */
5157                 tb->dirty_line = tb->cy;
5158
5159                 /* Expressions may need re-evaluation */
5160                 check_expression_line(tb, tb->cy);
5161
5162                 /* Text is changed */
5163                 tb->changed = TRUE;
5164
5165                 break;
5166         }
5167
5168         case EC_SEARCH_STR:
5169         {
5170                 byte search_dir;
5171
5172                 /* Become dirty because of item/equip menu */
5173                 tb->dirty_flags |= DIRTY_SCREEN;
5174
5175                 search_dir = get_string_for_search(&tb->search_o_ptr, &tb->search_str);
5176
5177                 if (!search_dir) break;
5178
5179                 if (search_dir == 1) do_editor_command(tb, EC_SEARCH_FORW);
5180                 else do_editor_command(tb, EC_SEARCH_BACK);
5181                 break;
5182         }
5183
5184         case EC_SEARCH_FORW:
5185                 if (tb->search_o_ptr)
5186                 {
5187                         if (!search_for_object(tb, tb->search_o_ptr, TRUE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5188                 }
5189                 else if (tb->search_str)
5190                 {
5191                         if (!search_for_string(tb, tb->search_str, TRUE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5192                 }
5193                 else
5194                 {
5195                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5196                 }
5197                 break;
5198
5199         case EC_SEARCH_BACK:
5200                 if (tb->search_o_ptr)
5201                 {
5202                         if (!search_for_object(tb, tb->search_o_ptr, FALSE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5203                 }
5204                 else if (tb->search_str)
5205                 {
5206                         if (!search_for_string(tb, tb->search_str, FALSE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5207                 }
5208                 else
5209                 {
5210                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5211                 }
5212                 break;
5213
5214         case EC_SEARCH_OBJ:
5215                 /* Become dirty because of item/equip menu */
5216                 tb->dirty_flags |= DIRTY_SCREEN;
5217
5218                 if (!get_object_for_search(&tb->search_o_ptr, &tb->search_str)) break;
5219
5220                 do_editor_command(tb, EC_SEARCH_FORW);
5221                 break;
5222
5223         case EC_SEARCH_DESTROYED:
5224                 if (!get_destroyed_object_for_search(&tb->search_o_ptr, &tb->search_str)) break;
5225
5226                 do_editor_command(tb, EC_SEARCH_FORW);
5227                 break;
5228
5229         case EC_INSERT_OBJECT:
5230         {
5231                 /* Insert choosen item name */
5232
5233                 autopick_type an_entry, *entry = &an_entry;
5234
5235                 if (!entry_from_choosed_object(entry))
5236                 {
5237                         /* Now dirty because of item/equip menu */
5238                         tb->dirty_flags |= DIRTY_SCREEN;
5239                         break;
5240                 }
5241
5242                 tb->cx = 0;
5243                 insert_return_code(tb);
5244                 string_free(tb->lines_list[tb->cy]);
5245                 tb->lines_list[tb->cy] = autopick_line_from_entry_kill(entry);
5246
5247                 /* Now dirty because of item/equip menu */
5248                 tb->dirty_flags |= DIRTY_SCREEN;
5249
5250                 break;
5251         }
5252
5253         case EC_INSERT_DESTROYED:
5254                 /* Insert a name of last destroyed item */
5255                 if (tb->last_destroyed)
5256                 {
5257                         tb->cx = 0;
5258                         insert_return_code(tb);
5259                         string_free(tb->lines_list[tb->cy]);
5260                         tb->lines_list[tb->cy] = string_make(tb->last_destroyed);
5261
5262                         /* Now dirty */
5263                         tb->dirty_flags |= DIRTY_ALL;
5264
5265                         /* Text is changed */
5266                         tb->changed = TRUE;
5267                 }
5268                 break;
5269
5270         case EC_INSERT_BLOCK:
5271         {
5272                 /* Insert a conditinal expression line */
5273                 char classrace[80];
5274
5275                 /* Conditional Expression for Class and Race */
5276                 sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
5277 #ifdef JP
5278                         rp_ptr->E_title, cp_ptr->E_title
5279 #else
5280                         rp_ptr->title, cp_ptr->title
5281 #endif
5282                         );
5283
5284                 tb->cx = 0;
5285                 insert_return_code(tb);
5286                 string_free(tb->lines_list[tb->cy]);
5287                 tb->lines_list[tb->cy] = string_make(classrace);
5288                 tb->cy++;
5289                 insert_return_code(tb);
5290                 string_free(tb->lines_list[tb->cy]);
5291                 tb->lines_list[tb->cy] = string_make("?:1");
5292
5293                 /* Now dirty */
5294                 tb->dirty_flags |= DIRTY_ALL;
5295
5296                 /* Text is changed */
5297                 tb->changed = TRUE;
5298
5299                 break;
5300         }
5301
5302         case EC_INSERT_MACRO:
5303                 /* Draw_everythig (delete menu) */
5304                 draw_text_editor(tb);
5305
5306                 /* Erase line */
5307                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5308
5309                 /* Prompt */
5310 #ifdef JP
5311                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<¥È¥ê¥¬¡¼¥­¡¼>: ");
5312 #else
5313                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<Trigger key>: ");
5314 #endif
5315                 if (insert_macro_line(tb))
5316                 {
5317                         /* Prepare to input action */
5318                         tb->cx = 2;
5319
5320                         /* Now dirty */
5321                         tb->dirty_flags |= DIRTY_ALL;
5322
5323                         /* Text is changed */
5324                         tb->changed = TRUE;
5325                 }
5326
5327                 break;
5328
5329         case EC_INSERT_KEYMAP:
5330                 /* Draw_everythig (delete menu) */
5331                 draw_text_editor(tb);
5332
5333                 /* Erase line */
5334                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5335
5336                 /* Prompt */
5337 #ifdef JP
5338                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, format("C:%d:<¥³¥Þ¥ó¥É¥­¡¼>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
5339 #else
5340                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, format("C:%d:<Keypress>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
5341 #endif
5342
5343                 if (insert_keymap_line(tb))
5344                 {
5345                         /* Prepare to input action */
5346                         tb->cx = 2;
5347
5348                         /* Now dirty */
5349                         tb->dirty_flags |= DIRTY_ALL;
5350
5351                         /* Text is changed */
5352                         tb->changed = TRUE;
5353                 }                               
5354                 break;
5355
5356         case EC_CL_AUTOPICK: toggle_command_letter(tb, DO_AUTOPICK); break;
5357         case EC_CL_DESTROY: toggle_command_letter(tb, DO_AUTODESTROY); break;
5358         case EC_CL_LEAVE: toggle_command_letter(tb, DONT_AUTOPICK); break;
5359         case EC_CL_QUERY: toggle_command_letter(tb, DO_QUERY_AUTOPICK); break;
5360         case EC_CL_NO_DISP: toggle_command_letter(tb, DO_DISPLAY); break;
5361
5362         case EC_IK_UNAWARE: toggle_keyword(tb, FLG_UNAWARE); break;
5363         case EC_IK_UNIDENTIFIED: toggle_keyword(tb, FLG_UNIDENTIFIED); break;
5364         case EC_IK_IDENTIFIED: toggle_keyword(tb, FLG_IDENTIFIED); break;
5365         case EC_IK_STAR_IDENTIFIED: toggle_keyword(tb, FLG_STAR_IDENTIFIED); break;
5366         case EC_KK_WEAPONS: toggle_keyword(tb, FLG_WEAPONS); break;
5367         case EC_KK_FAVORITE: toggle_keyword(tb, FLG_FAVORITE); break;
5368         case EC_KK_ARMORS: toggle_keyword(tb, FLG_ARMORS); break;
5369         case EC_KK_MISSILES: toggle_keyword(tb, FLG_MISSILES); break;
5370         case EC_KK_DEVICES: toggle_keyword(tb, FLG_DEVICES); break;
5371         case EC_KK_LIGHTS: toggle_keyword(tb, FLG_LIGHTS); break;
5372         case EC_KK_JUNKS: toggle_keyword(tb, FLG_JUNKS); break;
5373         case EC_KK_SPELLBOOKS: toggle_keyword(tb, FLG_SPELLBOOKS); break;
5374         case EC_KK_SHIELDS: toggle_keyword(tb, FLG_SHIELDS); break;
5375         case EC_KK_BOWS: toggle_keyword(tb, FLG_BOWS); break;
5376         case EC_KK_RINGS: toggle_keyword(tb, FLG_RINGS); break;
5377         case EC_KK_AMULETS: toggle_keyword(tb, FLG_AMULETS); break;
5378         case EC_KK_SUITS: toggle_keyword(tb, FLG_SUITS); break;
5379         case EC_KK_CLOAKS: toggle_keyword(tb, FLG_CLOAKS); break;
5380         case EC_KK_HELMS: toggle_keyword(tb, FLG_HELMS); break;
5381         case EC_KK_GLOVES: toggle_keyword(tb, FLG_GLOVES); break;
5382         case EC_KK_BOOTS: toggle_keyword(tb, FLG_BOOTS); break;
5383         case EC_OK_COLLECTING: toggle_keyword(tb, FLG_COLLECTING); break;
5384         case EC_OK_BOOSTED: toggle_keyword(tb, FLG_BOOSTED); break;
5385         case EC_OK_MORE_THAN: toggle_keyword(tb, FLG_MORE_THAN); break;
5386         case EC_OK_MORE_BONUS: toggle_keyword(tb, FLG_MORE_BONUS); break;
5387         case EC_OK_WORTHLESS: toggle_keyword(tb, FLG_WORTHLESS); break;
5388         case EC_OK_ARTIFACT: toggle_keyword(tb, FLG_ARTIFACT); break;
5389         case EC_OK_EGO: toggle_keyword(tb, FLG_EGO); break;
5390         case EC_OK_NAMELESS: toggle_keyword(tb, FLG_NAMELESS); break;
5391         case EC_OK_WANTED: toggle_keyword(tb, FLG_WANTED); break;
5392         case EC_OK_UNIQUE: toggle_keyword(tb, FLG_UNIQUE); break;
5393         case EC_OK_HUMAN: toggle_keyword(tb, FLG_HUMAN); break;
5394         case EC_OK_UNREADABLE:
5395                 toggle_keyword(tb, FLG_UNREADABLE);
5396                 add_keyword(tb, FLG_SPELLBOOKS);
5397                 break;
5398         case EC_OK_REALM1:
5399                 toggle_keyword(tb, FLG_REALM1);
5400                 add_keyword(tb, FLG_SPELLBOOKS);
5401                 break;
5402         case EC_OK_REALM2:
5403                 toggle_keyword(tb, FLG_REALM2);
5404                 add_keyword(tb, FLG_SPELLBOOKS);
5405                 break;
5406         case EC_OK_FIRST:
5407                 toggle_keyword(tb, FLG_FIRST);
5408                 add_keyword(tb, FLG_SPELLBOOKS);
5409                 break;
5410         case EC_OK_SECOND:
5411                 toggle_keyword(tb, FLG_SECOND);
5412                 add_keyword(tb, FLG_SPELLBOOKS);
5413                 break;
5414         case EC_OK_THIRD:
5415                 toggle_keyword(tb, FLG_THIRD);
5416                 add_keyword(tb, FLG_SPELLBOOKS);
5417                 break;
5418         case EC_OK_FOURTH:
5419                 toggle_keyword(tb, FLG_FOURTH);
5420                 add_keyword(tb, FLG_SPELLBOOKS);
5421                 break;
5422         }
5423
5424         /* Save old command */
5425         tb->old_com_id = com_id;
5426
5427         return FALSE;
5428 }
5429
5430
5431 /*
5432  * Insert single letter at cursor position.
5433  */
5434 static void insert_single_letter(text_body_type *tb, int key)
5435 {
5436         int i, j, len;
5437         char buf[MAX_LINELEN];
5438
5439         /* Save preceding string */
5440         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
5441                 buf[j++] = tb->lines_list[tb->cy][i];
5442
5443         /* Add a character */
5444 #ifdef JP
5445         if (iskanji(key))
5446         {
5447                 int next;
5448
5449                 inkey_base = TRUE;
5450                 next = inkey();
5451                 if (j+2 < MAX_LINELEN)
5452                 {
5453                         buf[j++] = key;
5454                         buf[j++] = next;
5455                         tb->cx += 2;
5456                 }
5457                 else
5458                         bell();
5459         }
5460         else
5461 #endif
5462         {
5463                 if (j+1 < MAX_LINELEN)
5464                         buf[j++] = key;
5465                 tb->cx++;
5466         }
5467
5468         /* Add following */
5469         for (; tb->lines_list[tb->cy][i] && j + 1 < MAX_LINELEN; i++)
5470                 buf[j++] = tb->lines_list[tb->cy][i];
5471         buf[j] = '\0';
5472
5473         /* Replace current line with new line */
5474         string_free(tb->lines_list[tb->cy]);
5475         tb->lines_list[tb->cy] = string_make(buf);
5476
5477         /* Move to correct collumn */
5478         len = strlen(tb->lines_list[tb->cy]);
5479         if (len < tb->cx) tb->cx = len;
5480
5481         /* Now dirty */
5482         tb->dirty_line = tb->cy;
5483
5484         /* Expressions may need re-evaluation */
5485         check_expression_line(tb, tb->cy);
5486
5487         /* Text is changed */
5488         tb->changed = TRUE;
5489 }
5490
5491
5492 /*
5493  * Check special key code and get a movement command id
5494  */
5495 static int analyze_move_key(text_body_type *tb, int skey)
5496 {
5497         int com_id;
5498
5499         /* Not a special key */
5500         if (!(skey & SKEY_MASK)) return 0;
5501
5502         /* Convert from a special key code to an editor command */
5503         switch(skey & ~SKEY_MOD_MASK)
5504         {
5505         case SKEY_DOWN:   com_id = EC_DOWN;   break;
5506         case SKEY_LEFT:   com_id = EC_LEFT;   break;
5507         case SKEY_RIGHT:  com_id = EC_RIGHT;  break;
5508         case SKEY_UP:     com_id = EC_UP;     break;
5509         case SKEY_PGUP:   com_id = EC_PGUP;   break;
5510         case SKEY_PGDOWN: com_id = EC_PGDOWN; break;
5511         case SKEY_TOP:    com_id = EC_TOP;    break;
5512         case SKEY_BOTTOM: com_id = EC_BOTTOM; break;
5513
5514         default:
5515                 /* Not a special movement key */
5516                 return 0;
5517         }
5518
5519         /* Without shift modifier */
5520         if (!(skey & SKEY_MOD_SHIFT))
5521         {
5522                 /*
5523                  * Un-shifted cursor keys cancells
5524                  * selection created by shift+cursor.
5525                  */
5526                 if (tb->mark & MARK_BY_SHIFT)
5527                 {
5528                         tb->mark = 0;
5529
5530                         /* Now dirty */
5531                         tb->dirty_flags |= DIRTY_ALL;
5532                 }
5533         }
5534
5535         /* With shift modifier */
5536         else
5537         {
5538                 /* Start selection by shift + cursor keys */
5539                 if (!tb->mark)
5540                 {
5541                         int len = strlen(tb->lines_list[tb->cy]);
5542
5543                         tb->mark = MARK_MARK | MARK_BY_SHIFT;
5544                         tb->my = tb->cy;
5545                         tb->mx = tb->cx;
5546
5547                         /* Correct cursor location */
5548                         if (tb->cx > len) tb->mx = len;
5549                                                 
5550                         /* Need to redraw text */
5551                         if (com_id == EC_UP || com_id == EC_DOWN)
5552                         {
5553                                 /* Redraw all text */
5554                                 tb->dirty_flags |= DIRTY_ALL;
5555                         }
5556                         else
5557                         {
5558                                 tb->dirty_line = tb->cy;
5559                         }
5560                 }
5561         }
5562
5563         return com_id;
5564 }
5565
5566 /*
5567  * In-game editor of Object Auto-picker/Destoryer
5568  */
5569 void do_cmd_edit_autopick(void)
5570 {
5571         static int cx_save = 0;
5572         static int cy_save = 0;
5573
5574         text_body_type text_body, *tb = &text_body;
5575
5576         autopick_type an_entry, *entry = &an_entry;
5577         char buf[MAX_LINELEN];
5578
5579         int i;
5580         int key = -1;
5581
5582         static s32b old_autosave_turn = 0L;
5583         byte quit = 0;
5584
5585         tb->changed = FALSE;
5586         tb->cx = cx_save;
5587         tb->cy = cy_save;
5588         tb->upper = tb->left = 0;
5589         tb->mark = 0;
5590         tb->mx = tb->my = 0;
5591         tb->old_cy = tb->old_upper = tb->old_left = -1;
5592         tb->old_wid = tb->old_hgt = -1;
5593         tb->old_com_id = 0;
5594
5595         tb->yank = NULL;
5596         tb->search_o_ptr = NULL;
5597         tb->search_str = NULL;
5598         tb->last_destroyed = NULL;
5599         tb->dirty_flags = DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
5600         tb->dirty_line = -1;
5601         tb->filename_mode = PT_DEFAULT;
5602
5603         /* Autosave */
5604         if (turn > old_autosave_turn + 100L)
5605         {
5606                 do_cmd_save_game(TRUE);
5607                 old_autosave_turn = turn;
5608         }
5609
5610         /* HACK -- Reset start_time to stop counting playtime while edit */
5611         update_playtime();
5612
5613         /* Free old entries */
5614         init_autopicker();
5615
5616         /* Command Description of the 'Last Destroyed Item' */
5617         if (autopick_last_destroyed_object.k_idx)
5618         {
5619                 autopick_entry_from_object(entry, &autopick_last_destroyed_object);
5620                 tb->last_destroyed = autopick_line_from_entry_kill(entry);
5621         }
5622
5623         /* Read or initialize whole text */
5624         tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
5625
5626         /* Reset cursor position if needed */
5627         for (i = 0; i < tb->cy; i++)
5628         {
5629                 if (!tb->lines_list[i])
5630                 {
5631                         tb->cy = tb->cx = 0;
5632                         break;
5633                 }
5634         }
5635
5636         /* Save the screen */
5637         screen_save();
5638
5639         /* Process requests until done */
5640         while (!quit)
5641         {
5642                 int com_id = 0;
5643
5644                 /* Draw_everythig */
5645                 draw_text_editor(tb);
5646
5647                 /* Display header line */
5648 #ifdef JP
5649                 prt("(^Q:½ªÎ» ^W:¥»¡¼¥Ö¤·¤Æ½ªÎ», ESC:¥á¥Ë¥å¡¼, ¤½¤Î¾:ÆþÎÏ)", 0, 0);
5650 #else   
5651                 prt("(^Q:quit, ^W:save&quit, ESC:menu, Other:input text)", 0, 0);
5652 #endif
5653                 if (!tb->mark)
5654                 {
5655                         /* Display current position */
5656                         prt (format("(%d,%d)", tb->cx, tb->cy), 0, 60);
5657                 }
5658                 else
5659                 {
5660                         /* Display current position and mark position */
5661                         prt (format("(%d,%d)-(%d,%d)", tb->mx, tb->my, tb->cx, tb->cy), 0, 60);
5662                 }
5663
5664                 /* Place cursor */
5665                 Term_gotoxy(tb->cx - tb->left, tb->cy - tb->upper + 1);
5666
5667                 /* Now clean */
5668                 tb->dirty_flags = 0;
5669                 tb->dirty_line = -1;
5670
5671                 /* Save old key and location */
5672                 tb->old_cy = tb->cy;
5673                 tb->old_upper = tb->upper;
5674                 tb->old_left = tb->left;
5675                 tb->old_wid = tb->wid;
5676                 tb->old_hgt = tb->hgt;
5677
5678                 /* Get a command */
5679                 key = inkey_special();
5680
5681                 /* Special keys */
5682                 if (key & SKEY_MASK)
5683                 {
5684                         /* Get a movement command */
5685                         com_id = analyze_move_key(tb, key);
5686                 }
5687
5688                 /* Open the menu */
5689                 else if (key == ESCAPE)
5690                 {
5691                         com_id = do_command_menu(0, 0);
5692
5693                         /* Redraw all text later */
5694                         tb->dirty_flags |= DIRTY_SCREEN;
5695                 }
5696
5697                 /* Insert a character */
5698                 else if (!iscntrl((unsigned char)key))
5699                 {
5700                         /* Ignore selection */
5701                         if (tb->mark)
5702                         {
5703                                 tb->mark = 0;
5704
5705                                 /* Now dirty */
5706                                 tb->dirty_flags |= DIRTY_ALL;
5707                         }
5708
5709                         insert_single_letter(tb, key);
5710
5711                         /* Next loop */
5712                         continue;
5713                 }
5714
5715                 /* Other commands */
5716                 else
5717                 {
5718                         com_id = get_com_id(key);
5719                 }
5720
5721                 if (com_id) quit = do_editor_command(tb, com_id);
5722         } /* while (1) */
5723
5724         /* Restore the screen */
5725         screen_load();
5726
5727         /* Get the filename of preference */
5728         strcpy(buf, pickpref_filename(tb->filename_mode));
5729
5730         if (quit == QUIT_AND_SAVE)
5731                 write_text_lines(buf, tb->lines_list);
5732
5733         free_text_lines(tb->lines_list);
5734
5735         string_free(tb->last_destroyed);
5736
5737         /* Destroy string chain */
5738         kill_yank_chain(tb);
5739
5740         /* Reload autopick pref */
5741         process_pickpref_file(buf);
5742
5743         /* HACK -- reset start_time so that playtime is not increase while edit */
5744         start_time = time(NULL);
5745
5746         /* Save cursor location */
5747         cx_save = tb->cx;
5748         cy_save = tb->cy;
5749 }