OSDN Git Service

一度も自動破壊されてない時に、メニューから「自動破壊されたアイテムで検索」を選んだ時、エラーメッセージが出るように修正。
[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 (TRUE)
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  * Convert string to lower case
1277  */
1278 static void str_tolower(char *str)
1279 {
1280         /* Force to be lower case string */
1281         for (; *str; str++)
1282         {
1283 #ifdef JP
1284                 if (iskanji(*str))
1285                 {
1286                         str++;
1287                         continue;
1288                 }
1289 #endif
1290                 *str = tolower(*str);
1291         }
1292 }
1293
1294
1295 /*
1296  * A function for Auto-picker/destroyer
1297  * Examine whether the object matches to the list of keywords or not.
1298  */
1299 int is_autopick(object_type *o_ptr)
1300 {
1301         int i;
1302         char o_name[MAX_NLEN];
1303
1304         if (o_ptr->tval == TV_GOLD) return -1;
1305
1306         /* Prepare object name string first */
1307         object_desc(o_name, o_ptr, FALSE, 3);
1308
1309         /* Convert the string to lower case */
1310         str_tolower(o_name);
1311
1312         /* Look for a matching entry in the list */     
1313         for (i=0; i < max_autopick; i++)
1314         {
1315                 autopick_type *entry = &autopick_list[i];
1316
1317                 if (is_autopick_aux(o_ptr, entry, o_name)) return i;
1318         }
1319
1320         /* No matching entry */
1321         return -1;
1322 }
1323
1324
1325 /*
1326  *  Auto inscription
1327  */
1328 static void auto_inscribe_item(object_type *o_ptr, int idx)
1329 {
1330         /* Are there auto-inscription? */
1331         if (idx < 0 || !autopick_list[idx].insc) return;
1332
1333         if (!o_ptr->inscription)
1334                 o_ptr->inscription = quark_add(autopick_list[idx].insc);
1335
1336         /* Redraw inscription */
1337         p_ptr->window |= (PW_EQUIP | PW_INVEN);
1338
1339         /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
1340         p_ptr->update |= (PU_BONUS);
1341 }
1342
1343
1344 /*
1345  * Automatically destroy items in this grid.
1346  */
1347 static bool is_opt_confirm_destroy(object_type *o_ptr)
1348 {
1349         if (!destroy_items) return FALSE;
1350
1351         /* Known to be worthless? */
1352         if (leave_worth)
1353                 if (object_value(o_ptr) > 0) return FALSE;
1354         
1355         if (leave_equip)
1356                 if ((o_ptr->tval >= TV_MISSILE_BEGIN) && (o_ptr->tval <= TV_ARMOR_END)) return FALSE;
1357         
1358         if (leave_chest)
1359                 if ((o_ptr->tval == TV_CHEST) && o_ptr->pval) return FALSE;
1360         
1361         if (leave_wanted)
1362         {
1363                 if (o_ptr->tval == TV_CORPSE
1364                     && object_is_shoukinkubi(o_ptr)) return FALSE;
1365         }
1366         
1367         if (leave_corpse)
1368                 if (o_ptr->tval == TV_CORPSE) return FALSE;
1369         
1370         if (leave_junk)
1371                 if ((o_ptr->tval == TV_SKELETON) || (o_ptr->tval == TV_BOTTLE) || (o_ptr->tval == TV_JUNK) || (o_ptr->tval == TV_STATUE)) return FALSE;
1372
1373         if (leave_special)
1374         {
1375                 if (p_ptr->prace == RACE_DEMON)
1376                 {
1377                         if (o_ptr->tval == TV_CORPSE &&
1378                             o_ptr->sval == SV_CORPSE &&
1379                             strchr("pht", r_info[o_ptr->pval].d_char))
1380                                 return FALSE;
1381                 }
1382
1383                 if (p_ptr->pclass == CLASS_ARCHER)
1384                 {
1385                         if (o_ptr->tval == TV_SKELETON ||
1386                             (o_ptr->tval == TV_CORPSE && o_ptr->sval == SV_SKELETON))
1387                                 return FALSE;
1388                 }
1389                 else if (p_ptr->pclass == CLASS_NINJA)
1390                 {
1391                         if (o_ptr->tval == TV_LITE &&
1392                             o_ptr->name2 == EGO_LITE_DARKNESS)
1393                                 return FALSE;
1394                 }
1395                 else if (p_ptr->pclass == CLASS_BEASTMASTER ||
1396                          p_ptr->pclass == CLASS_CAVALRY)
1397                 {
1398                         if (o_ptr->tval == TV_WAND &&
1399                             o_ptr->sval == SV_WAND_HEAL_MONSTER)
1400                                 return FALSE;
1401                 }
1402         }
1403         
1404         if (o_ptr->tval == TV_GOLD) return FALSE;
1405         
1406         return TRUE;
1407 }
1408
1409
1410 /*
1411  * Automatically destroy an item if it is to be destroyed
1412  *
1413  * When always_pickup is 'yes', we disable auto-destroyer function of
1414  * auto-picker/destroyer, and do only easy-auto-destroyer.
1415  */
1416 static object_type autopick_last_destroyed_object;
1417
1418 static void auto_destroy_item(object_type *o_ptr, int autopick_idx)
1419 {
1420         bool destroy = FALSE;
1421
1422         /* Easy-Auto-Destroyer (3rd priority) */
1423         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
1424
1425         /* Protected by auto-picker (2nd priotity) */
1426         if (autopick_idx >= 0 &&
1427             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
1428                 destroy = FALSE;
1429
1430         /* Auto-destroyer works only when !always_pickup */
1431         if (!always_pickup)
1432         {
1433                 /* Auto-picker/destroyer (1st priority) */
1434                 if (autopick_idx >= 0 &&
1435                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
1436                         destroy = TRUE;
1437         }
1438
1439         /* Not to be destroyed */
1440         if (!destroy) return;
1441
1442         /* Now decided to destroy */
1443
1444         disturb(0,0);
1445
1446         /* Artifact? */
1447         if (!can_player_destroy_object(o_ptr))
1448         {
1449                 char o_name[MAX_NLEN];
1450
1451                 /* Describe the object (with {terrible/special}) */
1452                 object_desc(o_name, o_ptr, TRUE, 3);
1453
1454                 /* Message */
1455 #ifdef JP
1456                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
1457 #else
1458                 msg_format("You cannot auto-destroy %s.", o_name);
1459 #endif
1460
1461                 /* Done */
1462                 return;
1463         }
1464
1465         /* Record name of destroyed item */
1466         COPY(&autopick_last_destroyed_object, o_ptr, object_type);
1467
1468         /* Destroy Later */
1469         o_ptr->marked |= OM_AUTODESTROY;
1470         p_ptr->notice |= PN_AUTODESTROY;
1471
1472         return;
1473 }
1474
1475
1476 /*
1477  *  Auto-destroy marked item
1478  */
1479 static void delayed_auto_destroy_aux(int item)
1480 {
1481         object_type *o_ptr;
1482
1483         /* Get the item (in the pack) */
1484         if (item >= 0) o_ptr = &inventory[item];
1485
1486         /* Get the item (on the floor) */
1487         else o_ptr = &o_list[0 - item];
1488
1489         if (o_ptr->k_idx && o_ptr->marked & OM_AUTODESTROY)
1490         {
1491                 char o_name[MAX_NLEN];
1492
1493                 /* Describe the object (with {terrible/special}) */
1494                 object_desc(o_name, o_ptr, TRUE, 3);
1495
1496                 /* Eliminate the item (from the pack) */
1497                 if (item >= 0)
1498                 {
1499                         inven_item_increase(item, -(o_ptr->number));
1500                         inven_item_optimize(item);
1501                 }
1502
1503                 /* Eliminate the item (from the floor) */
1504                 else
1505                 {
1506                         delete_object_idx(0 - item);
1507                 }
1508
1509                 /* Print a message */
1510 #ifdef JP
1511                 msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
1512 #else
1513                 msg_format("Auto-destroying %s.", o_name);
1514 #endif
1515         }
1516 }
1517
1518
1519 /*
1520  *  Auto-destroy marked items in inventry and on floor
1521  */
1522 void delayed_auto_destroy(void)
1523 {
1524         int item;
1525
1526         /* 
1527          * Scan inventry in reverse order to prevent
1528          * skipping after inven_item_optimize()
1529          */
1530         for (item = INVEN_TOTAL - 1; item >= 0 ; item--)
1531                 delayed_auto_destroy_aux(item);
1532
1533         /* Scan the pile of objects */
1534         item = cave[py][px].o_idx;
1535         while (item)
1536         {
1537                 int next = o_list[item].next_o_idx;
1538                 delayed_auto_destroy_aux(-item);
1539                 item = next;
1540         }
1541 }
1542
1543
1544 /*
1545  * Auto-inscription and/or destroy
1546  *
1547  * Auto-destroyer works only on inventory or on floor stack only when
1548  * requested.
1549  */
1550 void auto_do_item(int item, bool destroy)
1551 {
1552         object_type *o_ptr;
1553         int idx;
1554
1555         /* Get the item (in the pack) */
1556         if (item >= 0) o_ptr = &inventory[item];
1557
1558         /* Get the item (on the floor) */
1559         else o_ptr = &o_list[0 - item];
1560
1561         /* Get the index in the auto-pick/destroy list */
1562         idx = is_autopick(o_ptr);
1563
1564         /* Do auto-inscription */
1565         auto_inscribe_item(o_ptr, idx);
1566
1567         /* Do auto-destroy if needed */
1568         if (destroy && item <= INVEN_PACK)
1569                 auto_destroy_item(o_ptr, idx);
1570 }
1571
1572
1573 /*
1574  * Automatically pickup/destroy items in this grid.
1575  */
1576 void auto_pickup_items(cave_type *c_ptr)
1577 {
1578         s16b this_o_idx, next_o_idx = 0;
1579         
1580         /* Scan the pile of objects */
1581         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1582         {
1583                 int idx;
1584         
1585                 /* Acquire object */
1586                 object_type *o_ptr = &o_list[this_o_idx];
1587                 
1588                 /* Acquire next object */
1589                 next_o_idx = o_ptr->next_o_idx;
1590
1591                 /* Get the index of auto-picker list */
1592                 idx = is_autopick(o_ptr);
1593
1594                 /* Nothing defined on auto-picker? */
1595                 if (idx < 0) continue;
1596
1597                 /* Always do auto-inscription first */
1598                 auto_inscribe_item(o_ptr, idx);
1599
1600                 /* Do auto-destroy */
1601                 if (autopick_list[idx].action & DO_AUTODESTROY)
1602                 {
1603                         auto_destroy_item(o_ptr, idx);
1604                         continue;
1605                 }
1606
1607                 /* Disturb on auto-pick */
1608                 disturb(0,0);
1609
1610                 /* Is there an inventory space? */
1611                 if (!inven_carry_okay(o_ptr))
1612                 {
1613                         char o_name[MAX_NLEN];
1614
1615                         /* Describe the object */
1616                         object_desc(o_name, o_ptr, TRUE, 3);
1617
1618                         /* Message */
1619 #ifdef JP
1620                         msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1621 #else
1622                         msg_format("You have no room for %s.", o_name);
1623 #endif
1624                         /*
1625                          * Hack - No duplicate messages.
1626                          * remember that a message is given for the item here.
1627                          */
1628                         o_ptr->marked |= OM_NOMSG;
1629
1630                         continue;
1631                 }
1632
1633                 /* Ask if needed */
1634                 if (autopick_list[idx].action & DO_QUERY_AUTOPICK)
1635                 {
1636                         char out_val[MAX_NLEN+20];
1637                         char o_name[MAX_NLEN];
1638
1639                         if (o_ptr->marked & OM_NO_QUERY)
1640                         {
1641                                 /* Already answered as 'No' */
1642                                 continue;
1643                         }
1644
1645                         /* Describe the object */
1646                         object_desc(o_name, o_ptr, TRUE, 3);
1647
1648 #ifdef JP
1649                         sprintf(out_val, "%s¤ò½¦¤¤¤Þ¤¹¤«? ", o_name);
1650 #else
1651                         sprintf(out_val, "Pick up %s? ", o_name);
1652 #endif
1653
1654                         if (!get_check(out_val))
1655                         {
1656                                 /*
1657                                  * Hack - No duplicate messages/questions.
1658                                  * remember that a message is given
1659                                  * for the item here.
1660                                  */
1661                                 o_ptr->marked |= (OM_NOMSG | OM_NO_QUERY);
1662                                 continue;
1663                         }
1664
1665                 }
1666
1667                 /* Pick it up! */
1668                 py_pickup_aux(this_o_idx);
1669         }
1670 }
1671
1672
1673 #define PT_DEFAULT 0
1674 #define PT_WITH_PNAME 1
1675
1676 /*
1677  *  Get file name for autopick preference
1678  */
1679 static cptr pickpref_filename(int filename_mode)
1680 {
1681 #ifdef JP
1682         static const char namebase[] = "picktype";
1683 #else
1684         static const char namebase[] = "pickpref";
1685 #endif
1686
1687         switch (filename_mode)
1688         {
1689         case PT_DEFAULT:
1690                 return format("%s.prf", namebase);
1691
1692         case PT_WITH_PNAME:
1693                 return format("%s-%s.prf", namebase, player_name);
1694
1695         default:
1696                 return NULL;
1697         }
1698 }
1699
1700
1701 static const char autoregister_header[] = "?:$AUTOREGISTER";
1702
1703 /*
1704  *  Clear auto registered lines in the picktype.prf .
1705  */
1706 static bool clear_auto_register(void)
1707 {
1708         char tmp_file[1024];
1709         char pref_file[1024];
1710         char buf[1024];
1711         FILE *pref_fff;
1712         FILE *tmp_fff;
1713         int num = 0;
1714         bool autoregister = FALSE;
1715         bool okay = TRUE;
1716
1717         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1718         pref_fff = my_fopen(pref_file, "r");
1719
1720         if (!pref_fff)
1721         {
1722                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1723                 pref_fff = my_fopen(pref_file, "r");
1724         }
1725
1726         if (!pref_fff)
1727         {
1728                 /* No file yet */
1729                 return TRUE;
1730         }
1731
1732         /* Open a new (temporary) file */
1733         tmp_fff = my_fopen_temp(tmp_file, sizeof(tmp_file));
1734
1735         if (!tmp_fff)
1736         {
1737                 /* Close the preference file */
1738                 fclose(pref_fff);
1739
1740 #ifdef JP
1741                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", tmp_file);
1742 #else
1743                 msg_format("Failed to create temporary file %s.", tmp_file);
1744 #endif
1745                 msg_print(NULL);
1746                 return FALSE;
1747         }
1748
1749         
1750         /* Loop for every line */
1751         while (TRUE)
1752         {
1753                 /* Read a line */
1754                 if (my_fgets(pref_fff, buf, sizeof(buf))) break;
1755
1756                 if (autoregister)
1757                 {
1758                         /* Delete auto-registered line */
1759
1760                         /* Count auto-destroy preference lines */
1761                         if (buf[0] != '#' && buf[0] != '?') num++;
1762                 }
1763
1764                 /* We are looking for auto-registered line */
1765                 else
1766                 {
1767                         if (streq(buf, autoregister_header))
1768                         {
1769                                 /* Delete all further lines */
1770                                 autoregister = TRUE;
1771                         }
1772                         else
1773                         {
1774                                 /* Copy orginally lines */
1775                                 fprintf(tmp_fff, "%s\n", buf);
1776                         }
1777                 }
1778         }
1779
1780         /* Close files */
1781         my_fclose(pref_fff);
1782         my_fclose(tmp_fff);
1783
1784         if (num)
1785         {
1786 #ifdef JP
1787                 msg_format("°ÊÁ°¤Î¥­¥ã¥é¥¯¥¿¡¼ÍѤμ«Æ°ÀßÄê(%d¹Ô)¤¬»Ä¤Ã¤Æ¤¤¤Þ¤¹¡£", num);
1788                 strcpy(buf, "¸Å¤¤ÀßÄê¹Ô¤Ïºï½ü¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©");
1789 #else
1790                 msg_format("Auto registered lines (%d lines) for previous character are remaining.", num);
1791                 strcpy(buf, "These lines will be deleted.  Are you sure? ");
1792 #endif
1793
1794                 /* You can cancel it */
1795                 if (!get_check(buf))
1796                 {
1797                         okay = FALSE;
1798                         autoregister = FALSE;
1799
1800 #ifdef JP
1801                         msg_print("¥¨¥Ç¥£¥¿¤Î¥«¥Ã¥È&¥Ú¡¼¥¹¥ÈÅù¤ò»È¤Ã¤ÆɬÍפʹԤòÈòÆñ¤·¤Æ¤¯¤À¤µ¤¤¡£");
1802 #else
1803                         msg_print("Use cut & paste of auto picker editor (_) to keep old prefs.");
1804 #endif
1805                 }
1806         }
1807
1808
1809         /* If there are some changes, overwrite the original file with new one */
1810         if (autoregister)
1811         {
1812                 /* Copy contents of temporary file */
1813
1814                 tmp_fff = my_fopen(tmp_file, "r");
1815                 pref_fff = my_fopen(pref_file, "w");
1816
1817                 while (!my_fgets(tmp_fff, buf, sizeof(buf)))
1818                         fprintf(pref_fff, "%s\n", buf);
1819
1820                 my_fclose(pref_fff);
1821                 my_fclose(tmp_fff);
1822         }
1823
1824         /* Kill the temporary file */
1825         fd_kill(tmp_file);
1826
1827         return okay;
1828 }
1829
1830
1831 /*
1832  *  Automatically register an auto-destroy preference line
1833  */
1834 bool add_auto_register(object_type *o_ptr)
1835 {
1836         char buf[1024];
1837         char pref_file[1024];
1838         FILE *pref_fff;
1839         autopick_type an_entry, *entry = &an_entry;
1840
1841         int match_autopick = is_autopick(o_ptr);
1842
1843         /* Already registered */
1844         if (match_autopick != -1)
1845         {
1846                 cptr what;
1847                 byte act = autopick_list[match_autopick].action;
1848
1849 #ifdef JP
1850                 if (act & DO_AUTOPICK) what = "¼«Æ°¤Ç½¦¤¦";
1851                 else if (act & DO_AUTODESTROY) what = "¼«Æ°Ç˲õ¤¹¤ë";
1852                 else if (act & DONT_AUTOPICK) what = "ÊüÃÖ¤¹¤ë";
1853                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "³Îǧ¤·¤Æ½¦¤¦";
1854
1855                 msg_format("¤½¤Î¥¢¥¤¥Æ¥à¤Ï´û¤Ë%s¤è¤¦¤ËÀßÄꤵ¤ì¤Æ¤¤¤Þ¤¹¡£", what);
1856 #else
1857                 if (act & DO_AUTOPICK) what = "auto-pickup";
1858                 else if (act & DO_AUTODESTROY) what = "auto-destroy";
1859                 else if (act & DONT_AUTOPICK) what = "leave on floor";
1860                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "query auto-pickup";
1861
1862                 msg_format("The object is already registered to %s.", what);
1863 #endif
1864                 
1865                 return FALSE;
1866         }
1867
1868
1869         if (!p_ptr->autopick_autoregister)
1870         {
1871                 /* Clear old auto registered lines */
1872                 if (!clear_auto_register()) return FALSE;
1873         }
1874
1875         /* Try a filename with player name */
1876         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1877         pref_fff = my_fopen(pref_file, "r");
1878
1879         if (!pref_fff)
1880         {
1881                 /* Use default name */
1882                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1883                 pref_fff = my_fopen(pref_file, "r");
1884         }
1885
1886         /* Check the header */
1887         while (TRUE)
1888         {
1889                 /* Read a line */
1890                 if (my_fgets(pref_fff, buf, sizeof(buf)))
1891                 {
1892                         /* No header found */
1893                         p_ptr->autopick_autoregister = FALSE;
1894
1895                         break;
1896                 }
1897
1898                 if (streq(buf, autoregister_header))
1899                 {
1900                         /* Found the header */
1901                         p_ptr->autopick_autoregister = TRUE;
1902
1903                         break;
1904                 }
1905         }
1906
1907         /* Close read only FILE* */
1908         fclose(pref_fff);
1909
1910         /* Open for append */
1911         pref_fff = my_fopen(pref_file, "a");
1912
1913         /* Failure */
1914         if (!pref_fff) {
1915 #ifdef JP
1916                 msg_format("%s ¤ò³«¤¯¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", pref_file);
1917 #else
1918                 msg_format("Failed to open %s.", pref_file);
1919 #endif
1920                 msg_print(NULL);
1921
1922                 /* Failed */
1923                 return FALSE;
1924         }
1925
1926         if (!p_ptr->autopick_autoregister)
1927         {
1928                 /* Add the header */
1929                 fprintf(pref_fff, "%s\n", autoregister_header);
1930
1931 #ifdef JP
1932                 fprintf(pref_fff, "%s\n", "# *·Ù¹ð!!* °Ê¹ß¤Î¹Ô¤Ï¼«Æ°ÅÐÏ¿¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£");
1933                 fprintf(pref_fff, "%s\n", "# ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢É¬ÍפʹԤϾå¤ÎÊý¤Ø°ÜÆ°¤·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£");
1934 #else
1935                 fprintf(pref_fff, "%s\n", "# *Waring!* The lines below will be deleated later.");
1936                 fprintf(pref_fff, "%s\n", "# Keep it by cut & paste if you need these lines for future characters.");
1937 #endif
1938
1939                 /* Now auto register is in-use */
1940                 p_ptr->autopick_autoregister = TRUE;
1941         }
1942
1943         /* Get a preference entry */
1944         autopick_entry_from_object(entry, o_ptr);
1945
1946         /* Set to auto-destroy (with no-display) */
1947         entry->action = DO_AUTODESTROY;
1948
1949         /* Load the new line as preference */
1950         add_autopick_list(entry);
1951
1952         /* Add a line to the file */
1953         /* Don't kill "entry" */
1954         fprintf(pref_fff, "%s\n", autopick_line_from_entry(entry));
1955
1956         /* Close the file */
1957         fclose(pref_fff);
1958
1959         return TRUE;
1960 }
1961
1962
1963 /********  Auto-picker/destroyer editor  **********/
1964
1965 #define MAX_YANK MAX_LINELEN
1966 #define MAX_LINES 3000
1967
1968 #define MARK_MARK     0x01
1969 #define MARK_BY_SHIFT 0x02
1970
1971 #define LSTAT_BYPASS        0x01
1972 #define LSTAT_EXPRESSION    0x02
1973 #define LSTAT_AUTOREGISTER  0x04
1974
1975 #define QUIT_WITHOUT_SAVE 1
1976 #define QUIT_AND_SAVE     2
1977
1978 /* 
1979  * Struct for yank buffer
1980  */
1981 typedef struct chain_str {
1982         struct chain_str *next;
1983         char s[1];
1984 } chain_str_type;
1985
1986
1987 /*
1988  * Data struct for text editor
1989  */
1990 typedef struct {
1991         int wid, hgt;
1992         int cx, cy;
1993         int upper, left;
1994         int old_wid, old_hgt;
1995         int old_cy;
1996         int old_upper, old_left;
1997         int mx, my;
1998         byte mark;
1999
2000         object_type *search_o_ptr;
2001         cptr search_str;
2002         cptr last_destroyed;
2003
2004         chain_str_type *yank;
2005         bool yank_eol;
2006
2007         cptr *lines_list;
2008         byte states[MAX_LINES];
2009
2010         u16b dirty_flags;
2011         int dirty_line;
2012         int filename_mode;
2013         int old_com_id;
2014
2015         bool changed;
2016 } text_body_type;
2017
2018
2019 /*
2020  * Dirty flag for text editor
2021  */
2022 #define DIRTY_ALL           0x0001
2023 #define DIRTY_MODE          0x0004
2024 #define DIRTY_SCREEN        0x0008
2025 #define DIRTY_NOT_FOUND     0x0010
2026 #define DIRTY_NO_SEARCH     0x0020
2027 #define DIRTY_EXPRESSION    0x0040
2028 #define DIRTY_SKIP_INACTIVE 0x0080
2029 #define DIRTY_INACTIVE      0x0100
2030
2031 /*
2032  * Describe which kind of object is Auto-picked/destroyed
2033  */
2034 static void describe_autopick(char *buff, autopick_type *entry)
2035 {
2036         cptr str = entry->name;
2037         byte act = entry->action;
2038         cptr insc = entry->insc;
2039         int i;
2040
2041         bool top = FALSE;
2042
2043 #ifdef JP
2044         cptr before_str[100], body_str;
2045         int before_n = 0;
2046
2047         body_str = "¥¢¥¤¥Æ¥à";
2048
2049         /*** Collecting items ***/
2050         /*** Which can be absorbed into a slot as a bundle ***/
2051         if (IS_FLG(FLG_COLLECTING))
2052                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
2053         
2054         /*** Unidentified ***/
2055         if (IS_FLG(FLG_UNIDENTIFIED))
2056                 before_str[before_n++] = "̤´ÕÄê¤Î";
2057
2058         /*** Identified ***/
2059         if (IS_FLG(FLG_IDENTIFIED))
2060                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
2061
2062         /*** *Identified* ***/
2063         if (IS_FLG(FLG_STAR_IDENTIFIED))
2064                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
2065
2066         /*** Dice boosted (weapon of slaying) ***/
2067         if (IS_FLG(FLG_BOOSTED))
2068         {
2069                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
2070                 body_str = "Éð´ï";
2071         }
2072
2073         /*** Weapons whose dd*ds is more than nn ***/
2074         if (IS_FLG(FLG_MORE_THAN))
2075         {
2076                 static char more_than_desc_str[] = "___";
2077                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
2078                 body_str = "Éð´ï";
2079                         
2080                 sprintf(more_than_desc_str,"%d", entry->dice);
2081                 before_str[before_n++] = more_than_desc_str;
2082                 before_str[before_n++] = "°Ê¾å¤Î";
2083         }
2084
2085         /*** Items whose magical bonus is more than nn ***/
2086         if (IS_FLG(FLG_MORE_BONUS))
2087         {
2088                 static char more_bonus_desc_str[] = "___";
2089                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
2090                         
2091                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
2092                 before_str[before_n++] = more_bonus_desc_str;
2093                 before_str[before_n++] = ")°Ê¾å¤Î";
2094         }
2095
2096         /*** Worthless items ***/
2097         if (IS_FLG(FLG_WORTHLESS))
2098                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
2099
2100         /*** Artifact ***/
2101         if (IS_FLG(FLG_ARTIFACT))
2102         {
2103                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
2104                 body_str = "ÁõÈ÷";
2105         }
2106
2107         /*** Ego ***/
2108         if (IS_FLG(FLG_EGO))
2109         {
2110                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
2111                 body_str = "ÁõÈ÷";
2112         }
2113
2114         /*** Nameless ***/
2115         if (IS_FLG(FLG_NAMELESS))
2116         {
2117                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
2118                 body_str = "ÁõÈ÷";
2119         }
2120
2121         /*** Unaware items ***/
2122         if (IS_FLG(FLG_UNAWARE))
2123                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
2124
2125         /*** Wanted monster's corpse/skeletons ***/
2126         if (IS_FLG(FLG_WANTED))
2127         {
2128                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
2129                 body_str = "»àÂΤä¹ü";
2130         }
2131
2132         /*** Human corpse/skeletons (for Daemon magic) ***/
2133         if (IS_FLG(FLG_HUMAN))
2134         {
2135                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
2136                 body_str = "»àÂΤä¹ü";
2137         }
2138
2139         /*** Unique monster's corpse/skeletons/statues ***/
2140         if (IS_FLG(FLG_UNIQUE))
2141         {
2142                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
2143                 body_str = "»àÂΤä¹ü";
2144         }
2145
2146         /*** Unreadable spellbooks ***/
2147         if (IS_FLG(FLG_UNREADABLE))
2148         {
2149                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
2150                 body_str = "ËâË¡½ñ";
2151         }
2152
2153         /*** First realm spellbooks ***/
2154         if (IS_FLG(FLG_REALM1))
2155         {
2156                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
2157                 body_str = "ËâË¡½ñ";
2158         }
2159
2160         /*** Second realm spellbooks ***/
2161         if (IS_FLG(FLG_REALM2))
2162         {
2163                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
2164                 body_str = "ËâË¡½ñ";
2165         }
2166
2167         /*** First rank spellbooks ***/
2168         if (IS_FLG(FLG_FIRST))
2169         {
2170                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
2171                 body_str = "ËâË¡½ñ";
2172         }
2173
2174         /*** Second rank spellbooks ***/
2175         if (IS_FLG(FLG_SECOND))
2176         {
2177                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
2178                 body_str = "ËâË¡½ñ";
2179         }
2180
2181         /*** Third rank spellbooks ***/
2182         if (IS_FLG(FLG_THIRD))
2183         {
2184                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
2185                 body_str = "ËâË¡½ñ";
2186         }
2187
2188         /*** Fourth rank spellbooks ***/
2189         if (IS_FLG(FLG_FOURTH))
2190         {
2191                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
2192                 body_str = "ËâË¡½ñ";
2193         }
2194
2195         /*** Items ***/
2196         if (IS_FLG(FLG_ITEMS))
2197                 ; /* Nothing to do */
2198         else if (IS_FLG(FLG_WEAPONS))
2199                 body_str = "Éð´ï";
2200         else if (IS_FLG(FLG_ARMORS))
2201                 body_str = "Ëɶñ";
2202         else if (IS_FLG(FLG_MISSILES))
2203                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
2204         else if (IS_FLG(FLG_DEVICES))
2205                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
2206         else if (IS_FLG(FLG_LIGHTS))
2207                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
2208         else if (IS_FLG(FLG_JUNKS))
2209                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
2210         else if (IS_FLG(FLG_SPELLBOOKS))
2211                 body_str = "ËâË¡½ñ";
2212         else if (IS_FLG(FLG_HAFTED))
2213                 body_str = "Æß´ï";
2214         else if (IS_FLG(FLG_SHIELDS))
2215                 body_str = "½â";
2216         else if (IS_FLG(FLG_BOWS))
2217                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
2218         else if (IS_FLG(FLG_RINGS))
2219                 body_str = "»ØÎØ";
2220         else if (IS_FLG(FLG_AMULETS))
2221                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
2222         else if (IS_FLG(FLG_SUITS))
2223                 body_str = "³»";
2224         else if (IS_FLG(FLG_CLOAKS))
2225                 body_str = "¥¯¥í¡¼¥¯";
2226         else if (IS_FLG(FLG_HELMS))
2227                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
2228         else if (IS_FLG(FLG_GLOVES))
2229                 body_str = "äƼê";
2230         else if (IS_FLG(FLG_BOOTS))
2231                 body_str = "¥Ö¡¼¥Ä";
2232         else if (IS_FLG(FLG_FAVORITE))
2233                 body_str = "ÆÀ°ÕÉð´ï";
2234
2235         *buff = '\0';
2236         if (!before_n) 
2237                 strcat(buff, "Á´¤Æ¤Î");
2238         else for (i = 0; i < before_n && before_str[i]; i++)
2239                 strcat(buff, before_str[i]);
2240
2241         strcat(buff, body_str);
2242
2243         if (*str)
2244         {
2245                 if (*str == '^')
2246                 {
2247                         str++;
2248                         top = TRUE;
2249                 }
2250
2251                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
2252                 strncat(buff, str, 80);
2253                 if (top)
2254                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
2255                 else
2256                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
2257         }
2258
2259         if (insc)
2260         {
2261                 strncat(buff, format("¤Ë¡Ö%s¡×", insc), 80);
2262
2263                 if (strstr(insc, "%%all"))
2264                         strcat(buff, "(%%all¤ÏÁ´Ç½ÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2265                 else if (strstr(insc, "%all"))
2266                         strcat(buff, "(%all¤ÏÁ´Ç½ÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2267                 else if (strstr(insc, "%%"))
2268                         strcat(buff, "(%%¤ÏÄɲÃǽÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2269                 else if (strstr(insc, "%"))
2270                         strcat(buff, "(%¤ÏÄɲÃǽÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2271
2272                 strcat(buff, "¤È¹ï¤ó¤Ç");
2273         }
2274         else
2275                 strcat(buff, "¤ò");
2276
2277         if (act & DONT_AUTOPICK)
2278                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
2279         else if (act & DO_AUTODESTROY)
2280                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
2281         else if (act & DO_QUERY_AUTOPICK)
2282                 strcat(buff, "³Îǧ¤Î¸å¤Ë½¦¤¦¡£");
2283         else
2284                 strcat(buff, "½¦¤¦¡£");
2285
2286         if (act & DO_DISPLAY)
2287         {
2288                 if (act & DONT_AUTOPICK)
2289                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2290                 else if (act & DO_AUTODESTROY)
2291                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2292                 else
2293                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2294         }
2295         else
2296                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤¡£");
2297
2298 #else /* JP */
2299
2300         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
2301         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
2302
2303         body_str = "items";
2304
2305         /*** Collecting items ***/
2306         /*** Which can be absorbed into a slot as a bundle ***/
2307         if (IS_FLG(FLG_COLLECTING))
2308                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
2309         
2310         /*** Unidentified ***/
2311         if (IS_FLG(FLG_UNIDENTIFIED))
2312                 before_str[before_n++] = "unidentified";
2313
2314         /*** Identified ***/
2315         if (IS_FLG(FLG_IDENTIFIED))
2316                 before_str[before_n++] = "identified";
2317
2318         /*** *Identified* ***/
2319         if (IS_FLG(FLG_STAR_IDENTIFIED))
2320                 before_str[before_n++] = "fully identified";
2321
2322         /*** Worthless items ***/
2323         if (IS_FLG(FLG_WORTHLESS))
2324         {
2325                 before_str[before_n++] = "worthless";
2326                 which_str[which_n++] = "can not be sold at stores";
2327         }
2328
2329         /*** Artifacto ***/
2330         if (IS_FLG(FLG_ARTIFACT))
2331         {
2332                 before_str[before_n++] = "artifact";
2333         }
2334
2335         /*** Ego ***/
2336         if (IS_FLG(FLG_EGO))
2337         {
2338                 before_str[before_n++] = "ego";
2339         }
2340
2341         /*** Nameless ***/
2342         if (IS_FLG(FLG_NAMELESS))
2343         {
2344                 body_str = "equipment";
2345                 which_str[which_n++] = "is neither ego-item nor artifact";
2346         }
2347
2348         /*** Unaware items ***/
2349         if (IS_FLG(FLG_UNAWARE))
2350         {
2351                 before_str[before_n++] = "unidentified";
2352                 whose_str[whose_n++] = "basic abilities are not known";
2353         }
2354
2355         /*** Dice boosted (weapon of slaying) ***/
2356         if (IS_FLG(FLG_BOOSTED))
2357         {
2358                 body_str = "weapons";
2359                 whose_str[whose_n++] = "damage dice is bigger than normal";
2360         }
2361
2362         /*** Weapons whose dd*ds is more than nn ***/
2363         if (IS_FLG(FLG_MORE_THAN))
2364         {
2365                 static char more_than_desc_str[] =
2366                         "maximum damage from dice is bigger than __";
2367                 body_str = "weapons";
2368                         
2369                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
2370                         "%d", entry->dice);
2371                 whose_str[whose_n++] = more_than_desc_str;
2372         }
2373
2374         /*** Items whose magical bonus is more than nn ***/
2375         if (IS_FLG(FLG_MORE_BONUS))
2376         {
2377                 static char more_bonus_desc_str[] =
2378                         "magical bonus is bigger than (+__)";
2379                         
2380                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
2381                         "%d)", entry->bonus);
2382                 whose_str[whose_n++] = more_bonus_desc_str;
2383         }
2384
2385         /*** Wanted monster's corpse/skeletons ***/
2386         if (IS_FLG(FLG_WANTED))
2387         {
2388                 body_str = "corpse or skeletons";
2389                 which_str[which_n++] = "is wanted at the Hunter's Office";
2390         }
2391
2392         /*** Human corpse/skeletons (for Daemon magic) ***/
2393         if (IS_FLG(FLG_HUMAN))
2394         {
2395                 before_str[before_n++] = "humanoid";
2396                 body_str = "corpse or skeletons";
2397                 which_str[which_n++] = "can be used for Daemon magic";
2398         }
2399
2400         /*** Unique monster's corpse/skeletons/statues ***/
2401         if (IS_FLG(FLG_UNIQUE))
2402         {
2403                 before_str[before_n++] = "unique monster's";
2404                 body_str = "corpse or skeletons";
2405         }
2406
2407         /*** Unreadable spellbooks ***/
2408         if (IS_FLG(FLG_UNREADABLE))
2409         {
2410                 body_str = "spellbooks";
2411                 after_str[after_n++] = "of different realms from yours";
2412         }
2413
2414         /*** First realm spellbooks ***/
2415         if (IS_FLG(FLG_REALM1))
2416         {
2417                 body_str = "spellbooks";
2418                 after_str[after_n++] = "of your first realm";
2419         }
2420
2421         /*** Second realm spellbooks ***/
2422         if (IS_FLG(FLG_REALM2))
2423         {
2424                 body_str = "spellbooks";
2425                 after_str[after_n++] = "of your second realm";
2426         }
2427
2428         /*** First rank spellbooks ***/
2429         if (IS_FLG(FLG_FIRST))
2430         {
2431                 before_str[before_n++] = "first one of four";
2432                 body_str = "spellbooks";
2433         }
2434
2435         /*** Second rank spellbooks ***/
2436         if (IS_FLG(FLG_SECOND))
2437         {
2438                 before_str[before_n++] = "second one of four";
2439                 body_str = "spellbooks";
2440         }
2441
2442         /*** Third rank spellbooks ***/
2443         if (IS_FLG(FLG_THIRD))
2444         {
2445                 before_str[before_n++] = "third one of four";
2446                 body_str = "spellbooks";
2447         }
2448
2449         /*** Fourth rank spellbooks ***/
2450         if (IS_FLG(FLG_FOURTH))
2451         {
2452                 before_str[before_n++] = "fourth one of four";
2453                 body_str = "spellbooks";
2454         }
2455
2456         /*** Items ***/
2457         if (IS_FLG(FLG_ITEMS))
2458                 ; /* Nothing to do */
2459         else if (IS_FLG(FLG_WEAPONS))
2460                 body_str = "weapons";
2461         else if (IS_FLG(FLG_ARMORS))
2462                 body_str = "armors";
2463         else if (IS_FLG(FLG_MISSILES))
2464                 body_str = "shots, arrows or crossbow bolts";
2465         else if (IS_FLG(FLG_DEVICES))
2466                 body_str = "scrolls, wands, staves or rods";
2467         else if (IS_FLG(FLG_LIGHTS))
2468                 body_str = "light sources";
2469         else if (IS_FLG(FLG_JUNKS))
2470                 body_str = "junk such as broken sticks";
2471         else if (IS_FLG(FLG_SPELLBOOKS))
2472                 body_str = "spellbooks";
2473         else if (IS_FLG(FLG_HAFTED))
2474                 body_str = "hafted weapons";
2475         else if (IS_FLG(FLG_SHIELDS))
2476                 body_str = "shields";
2477         else if (IS_FLG(FLG_BOWS))
2478                 body_str = "slings, bows or crossbows";
2479         else if (IS_FLG(FLG_RINGS))
2480                 body_str = "rings";
2481         else if (IS_FLG(FLG_AMULETS))
2482                 body_str = "amulets";
2483         else if (IS_FLG(FLG_SUITS))
2484                 body_str = "body armors";
2485         else if (IS_FLG(FLG_CLOAKS))
2486                 body_str = "cloaks";
2487         else if (IS_FLG(FLG_HELMS))
2488                 body_str = "helms or crowns";
2489         else if (IS_FLG(FLG_GLOVES))
2490                 body_str = "gloves";
2491         else if (IS_FLG(FLG_BOOTS))
2492                 body_str = "boots";
2493         else if (IS_FLG(FLG_FAVORITE))
2494                 body_str = "favorite weapons";
2495
2496         /* Prepare a string for item name */
2497         if (*str)
2498         {
2499                 if (*str == '^')
2500                 {
2501                         str++;
2502                         top = TRUE;
2503                         whose_str[whose_n++] = "name is beginning with \"";
2504                 }
2505                 else
2506                         which_str[which_n++] = "have \"";
2507         }
2508
2509
2510         /* Describe action flag */
2511         if (act & DONT_AUTOPICK)
2512                 strcpy(buff, "Leave on floor ");
2513         else if (act & DO_AUTODESTROY)
2514                 strcpy(buff, "Destroy ");
2515         else if (act & DO_QUERY_AUTOPICK)
2516                 strcpy(buff, "Ask to pick up ");
2517         else
2518                 strcpy(buff, "Pickup ");
2519
2520         /* Auto-insctiption */
2521         if (insc)
2522         {
2523                 strncat(buff, format("and inscribe \"%s\"", insc), 80);
2524
2525                 if (strstr(insc, "%all"))
2526                         strcat(buff, ", replacing %all with code string representing all abilities,");
2527                 else if (strstr(insc, "%"))
2528                         strcat(buff, ", replacing % with code string representing extra random abilities,");
2529
2530                 strcat(buff, " on ");
2531         }
2532
2533         /* Adjective */
2534         if (!before_n) 
2535                 strcat(buff, "all ");
2536         else for (i = 0; i < before_n && before_str[i]; i++)
2537         {
2538                 strcat(buff, before_str[i]);
2539                 strcat(buff, " ");
2540         }
2541
2542         /* Item class */
2543         strcat(buff, body_str);
2544
2545         /* of ... */
2546         for (i = 0; i < after_n && after_str[i]; i++)
2547         {
2548                 strcat(buff, " ");
2549                 strcat(buff, after_str[i]);
2550         }
2551
2552         /* which ... */
2553         for (i = 0; i < whose_n && whose_str[i]; i++)
2554         {
2555                 if (i == 0)
2556                         strcat(buff, " whose ");
2557                 else
2558                         strcat(buff, ", and ");
2559
2560                 strcat(buff, whose_str[i]);
2561         }
2562
2563         /* Item name ; whose name is beginning with "str" */
2564         if (*str && top)
2565         {
2566                 strcat(buff, str);
2567                 strcat(buff, "\"");
2568         }
2569
2570         /* whose ..., and which .... */
2571         if (whose_n && which_n)
2572                 strcat(buff, ", and ");
2573
2574         /* which ... */
2575         for (i = 0; i < which_n && which_str[i]; i++)
2576         {
2577                 if (i == 0)
2578                         strcat(buff, " which ");
2579                 else
2580                         strcat(buff, ", and ");
2581
2582                 strcat(buff, which_str[i]);
2583         }
2584
2585         /* Item name ; which have "str" as part of its name */
2586         if (*str && !top)
2587         {
2588                 strncat(buff, str, 80);
2589                 strcat(buff, "\" as part of its name");
2590         }
2591         strcat(buff, ".");
2592
2593         /* Describe whether it will be displayed on the full map or not */
2594         if (act & DO_DISPLAY)
2595         {
2596                 if (act & DONT_AUTOPICK)
2597                         strcat(buff, "  Display these items when you press the N key in the full 'M'ap.");
2598                 else if (act & DO_AUTODESTROY)
2599                         strcat(buff, "  Display these items when you press the K key in the full 'M'ap.");
2600                 else
2601                         strcat(buff, "  Display these items when you press the M key in the full 'M'ap.");
2602         }
2603         else
2604                 strcat(buff, " Not displayed in the full map.");
2605 #endif /* JP */
2606
2607 }
2608
2609
2610 /*
2611  * Read whole lines of a file to memory
2612  */
2613 static cptr *read_text_lines(cptr filename, bool user)
2614 {
2615         cptr *lines_list = NULL;
2616         FILE *fff;
2617
2618         int lines = 0;
2619         char buf[1024];
2620
2621         if (user)
2622         {
2623                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2624         }
2625         else
2626         {
2627                 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
2628         }
2629         
2630         /* Open the file */
2631         fff = my_fopen(buf, "r");
2632
2633         if (fff)
2634         {
2635                 /* Allocate list of pointers */
2636                 C_MAKE(lines_list, MAX_LINES, cptr);
2637
2638                 /* Parse it */
2639                 while (0 == my_fgets(fff, buf, sizeof(buf)))
2640                 {
2641                         lines_list[lines++] = string_make(buf);
2642                         if (lines >= MAX_LINES - 1) break;
2643                 }
2644                 if (lines == 0)
2645                         lines_list[0] = string_make("");
2646
2647                 my_fclose(fff);
2648         }
2649
2650         if (!fff) return NULL;
2651         return lines_list;
2652 }
2653
2654
2655 static cptr *read_pickpref_text_lines(int *filename_mode_p)
2656 {
2657         char buf[1024];
2658         cptr *lines_list;
2659
2660         *filename_mode_p = PT_WITH_PNAME;
2661         strcpy(buf, pickpref_filename(*filename_mode_p));
2662         lines_list = read_text_lines(buf, TRUE);
2663
2664         if (!lines_list)
2665         {
2666                 *filename_mode_p = PT_DEFAULT;
2667                 strcpy(buf, pickpref_filename(*filename_mode_p));
2668                 lines_list = read_text_lines(buf, TRUE);
2669         }
2670
2671         if (!lines_list)
2672         {
2673                 strcpy(buf, pickpref_filename(*filename_mode_p));
2674                 lines_list = read_text_lines(buf, FALSE);
2675         }
2676
2677         if (!lines_list)
2678         {
2679                 /* Allocate list of pointers */
2680                 C_MAKE(lines_list, MAX_LINES, cptr);
2681                 lines_list[0] = string_make("");
2682         }
2683         return lines_list;
2684 }
2685
2686
2687 /*
2688  * Write whole lines of memory to a file.
2689  */
2690 static bool write_text_lines(cptr filename, cptr *lines_list)
2691 {
2692         FILE *fff;
2693
2694         int lines = 0;
2695         char buf[1024];
2696
2697         /* Build the filename */
2698         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2699         
2700         /* Open the file */
2701         fff = my_fopen(buf, "w");
2702         if (fff)
2703         {
2704                 for (lines = 0; lines_list[lines]; lines++)
2705                         my_fputs(fff, lines_list[lines], 1024);
2706
2707                 my_fclose(fff);
2708         }
2709
2710         if (!fff) return FALSE;
2711         return TRUE;
2712 }
2713
2714
2715 /*
2716  * Free memory of lines_list.
2717  */
2718 static void free_text_lines(cptr *lines_list)
2719 {
2720         int lines;
2721
2722         for (lines = 0; lines_list[lines]; lines++)
2723                 string_free(lines_list[lines]);
2724
2725         /* free list of pointers */
2726         C_FREE((char **)lines_list, MAX_LINES, char *);
2727 }
2728
2729
2730 /*
2731  * Delete or insert string
2732  */
2733 static void toggle_keyword(text_body_type *tb, int flg)
2734 {
2735         int by1, by2, bx1, bx2, y;
2736         bool add = TRUE;
2737         bool fixed = FALSE;
2738
2739         /* Select this line */
2740         if (!tb->mark)
2741         {
2742                 tb->my = tb->cy;
2743                 tb->mx = 0;
2744         }
2745
2746         if (tb->my < tb->cy)
2747         {
2748                 by1 = tb->my;
2749                 by2 = tb->cy;
2750                 bx1 = tb->mx;
2751                 bx2 = tb->cx;
2752         }
2753         else
2754         {
2755                 by2 = tb->my;
2756                 by1 = tb->cy;
2757                 bx2 = tb->mx;
2758                 bx1 = tb->cx;
2759         }
2760
2761         /* String on these lines are not really selected */  
2762         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2763         if (bx2 == 0 && by1 < by2) by2--;
2764
2765
2766         /* Set/Reset flag of each line */
2767         for (y = by1; y <= by2; y++)
2768         {
2769                 autopick_type an_entry, *entry = &an_entry;
2770
2771                 if (!autopick_new_entry(entry, tb->lines_list[y], !fixed)) continue;
2772
2773                 string_free(tb->lines_list[y]);
2774
2775                 if (!fixed)
2776                 {
2777                         /* Add? or Remove? */
2778                         if (!IS_FLG(flg)) add = TRUE;
2779                         else add = FALSE;
2780
2781                         /* No more change */
2782                         fixed = TRUE;
2783                 }
2784
2785                 /* You can use only one noun flag */
2786                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2787                 {
2788                         int i;
2789                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2790                                 REM_FLG(i);
2791                 }
2792                 
2793                 /* You can use only one identify state flag */
2794                 else if (FLG_UNAWARE <= flg && flg <= FLG_STAR_IDENTIFIED)
2795                 {
2796                         int i;
2797                         for (i = FLG_UNAWARE; i <= FLG_STAR_IDENTIFIED; i++)
2798                                 REM_FLG(i);
2799                 }
2800                 
2801                 /* You can use only one flag in artifact/ego/nameless */
2802                 else if (FLG_ARTIFACT <= flg && flg <= FLG_NAMELESS)
2803                 {
2804                         int i;
2805                         for (i = FLG_ARTIFACT; i <= FLG_NAMELESS; i++)
2806                                 REM_FLG(i);
2807                 }
2808                 
2809                 if (add) ADD_FLG(flg);
2810                 else REM_FLG(flg);
2811                 
2812                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2813                 
2814                 /* Now dirty */
2815                 tb->dirty_flags |= DIRTY_ALL;
2816
2817                 /* Text is changed */
2818                 tb->changed = TRUE;
2819         }
2820 }
2821
2822
2823 /*
2824  * Change command letter
2825  */
2826 static void toggle_command_letter(text_body_type *tb, byte flg)
2827 {
2828         autopick_type an_entry, *entry = &an_entry;
2829         int by1, by2, bx1, bx2, y;
2830         bool add = TRUE;
2831         bool fixed = FALSE;
2832
2833         /* Select this line */
2834         if (!tb->mark)
2835         {
2836                 tb->my = tb->cy;
2837                 tb->mx = 0;
2838         }
2839
2840         if (tb->my < tb->cy)
2841         {
2842                 by1 = tb->my;
2843                 by2 = tb->cy;
2844                 bx1 = tb->mx;
2845                 bx2 = tb->cx;
2846         }
2847         else
2848         {
2849                 by2 = tb->my;
2850                 by1 = tb->cy;
2851                 bx2 = tb->mx;
2852                 bx1 = tb->cx;
2853         }
2854
2855         /* String on these lines are not really selected */  
2856         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2857         if (bx2 == 0 && by1 < by2) by2--;
2858
2859         /* Set/Reset flag of each line */
2860         for (y = by1; y <= by2; y++)
2861         {
2862                 int wid = 0;
2863
2864                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2865
2866                 string_free(tb->lines_list[y]);
2867
2868                 if (!fixed)
2869                 {
2870                         /* Add? or Remove? */
2871                         if (!(entry->action & flg)) add = TRUE;
2872                         else add = FALSE;
2873
2874                         /* No more change */
2875                         fixed = TRUE;
2876                 }
2877
2878                 /* Count number of letter (by negative number) */
2879                 if (entry->action & DONT_AUTOPICK) wid--;
2880                 else if (entry->action & DO_AUTODESTROY) wid--;
2881                 else if (entry->action & DO_QUERY_AUTOPICK) wid--;
2882                 if (!(entry->action & DO_DISPLAY)) wid--;
2883
2884                 /* Set/Reset the flag */
2885                 if (flg != DO_DISPLAY)
2886                 {
2887                         entry->action &= ~(DO_AUTOPICK | DONT_AUTOPICK | DO_AUTODESTROY | DO_QUERY_AUTOPICK);
2888                         if (add) entry->action |= flg;
2889                         else entry->action |= DO_AUTOPICK;
2890                 }
2891                 else
2892                 {
2893                         entry->action &= ~(DO_DISPLAY);
2894                         if (add) entry->action |= flg;
2895                 }
2896
2897                 /* Correct cursor location */
2898                 if (tb->cy == y)
2899                 {
2900                         if (entry->action & DONT_AUTOPICK) wid++;
2901                         else if (entry->action & DO_AUTODESTROY) wid++;
2902                         else if (entry->action & DO_QUERY_AUTOPICK) wid++;
2903                         if (!(entry->action & DO_DISPLAY)) wid++;
2904
2905                         if (wid > 0) tb->cx++;
2906                         if (wid < 0 && tb->cx > 0) tb->cx--;
2907                 }
2908                         
2909                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2910                         
2911                 /* Now dirty */
2912                 tb->dirty_flags |= DIRTY_ALL;
2913
2914                 /* Text is changed */
2915                 tb->changed = TRUE;
2916         }
2917 }
2918
2919 /*
2920  * Delete or insert string
2921  */
2922 static void add_keyword(text_body_type *tb, int flg)
2923 {
2924         int by1, by2, bx1, bx2, y;
2925
2926         /* Select this line */
2927         if (!tb->mark)
2928         {
2929                 tb->my = tb->cy;
2930                 tb->mx = 0;
2931         }
2932
2933         if (tb->my < tb->cy)
2934         {
2935                 by1 = tb->my;
2936                 by2 = tb->cy;
2937                 bx1 = tb->mx;
2938                 bx2 = tb->cx;
2939         }
2940         else
2941         {
2942                 by2 = tb->my;
2943                 by1 = tb->cy;
2944                 bx2 = tb->mx;
2945                 bx1 = tb->cx;
2946         }
2947
2948         /* String on these lines are not really selected */  
2949         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2950         if (bx2 == 0 && by1 < by2) by2--;
2951
2952         /* Set/Reset flag of each line */
2953         for (y = by1; y <= by2; y++)
2954         {
2955                 autopick_type an_entry, *entry = &an_entry;
2956
2957                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2958
2959                 /* There is the flag already */
2960                 if (IS_FLG(flg))
2961                 {
2962                         /* Free memory for the entry */
2963                         autopick_free_entry(entry);
2964                         
2965                         continue;
2966                 }
2967                 
2968                 string_free(tb->lines_list[y]);
2969                 
2970                 /* Remove all noun flag */
2971                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2972                 {
2973                         int i;
2974                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2975                                 REM_FLG(i);
2976                 }
2977                 
2978                 ADD_FLG(flg);
2979                 
2980                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2981
2982                 /* Now dirty */
2983                 tb->dirty_flags |= DIRTY_ALL;
2984
2985                 /* Text is changed */
2986                 tb->changed = TRUE;
2987         }
2988 }
2989
2990
2991 /*
2992  * Check if this line is expression or not.
2993  * And update it if it is.
2994  */
2995 static void check_expression_line(text_body_type *tb, int y)
2996 {
2997         cptr s = tb->lines_list[y];
2998
2999         if ((s[0] == '?' && s[1] == ':') ||
3000             (tb->states[y] & LSTAT_BYPASS))
3001         {
3002                 /* Expressions need re-evaluation */
3003                 tb->dirty_flags |= DIRTY_EXPRESSION;
3004         }
3005 }
3006
3007
3008 /*
3009  * Insert return code and split the line
3010  */
3011 static bool insert_return_code(text_body_type *tb)
3012 {
3013         char buf[MAX_LINELEN];
3014         int i, j, k;
3015
3016         for (k = 0; tb->lines_list[k]; k++)
3017                 /* count number of lines */ ;
3018
3019         if (k >= MAX_LINES - 2) return FALSE;
3020         k--;
3021
3022         /* Move down lines */
3023         for (; tb->cy < k; k--)
3024         {
3025                 tb->lines_list[k+1] = tb->lines_list[k];
3026                 tb->states[k+1] = tb->states[k];
3027         }
3028
3029         /* Split current line */
3030         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
3031         {
3032 #ifdef JP
3033                 if (iskanji(tb->lines_list[tb->cy][i]))
3034                         buf[j++] = tb->lines_list[tb->cy][i++];
3035 #endif
3036                 buf[j++] = tb->lines_list[tb->cy][i];
3037         }
3038         buf[j] = '\0';
3039         tb->lines_list[tb->cy+1] = string_make(&tb->lines_list[tb->cy][i]);
3040         string_free(tb->lines_list[tb->cy]);
3041         tb->lines_list[tb->cy] = string_make(buf);
3042
3043         /* Expressions need re-evaluation */
3044         tb->dirty_flags |= DIRTY_EXPRESSION;
3045
3046         /* Text is changed */
3047         tb->changed = TRUE;
3048
3049         return TRUE;
3050 }
3051
3052
3053 /*
3054  * Choose an item and get auto-picker entry from it.
3055  */
3056 static object_type *choose_object(cptr q, cptr s)
3057 {
3058         int item;
3059
3060         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return NULL;
3061
3062         /* Get the item (in the pack) */
3063         if (item >= 0) return &inventory[item];
3064
3065         /* Get the item (on the floor) */
3066         else return &o_list[0 - item];
3067 }
3068
3069
3070 /*
3071  * Choose an item and get auto-picker entry from it.
3072  */
3073 static bool entry_from_choosed_object(autopick_type *entry)
3074 {
3075         object_type *o_ptr;
3076         cptr q, s;
3077
3078         /* Get an item */
3079 #ifdef JP
3080         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
3081         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3082 #else
3083         q = "Enter which item? ";
3084         s = "You have nothing to enter.";
3085 #endif
3086         o_ptr = choose_object(q, s);
3087         if (!o_ptr) return FALSE;
3088
3089         autopick_entry_from_object(entry, o_ptr);
3090         return TRUE;
3091 }
3092
3093
3094 /*
3095  * Choose an item for search
3096  */
3097 static byte get_object_for_search(object_type **o_handle, cptr *search_strp)
3098 {
3099         char buf[MAX_NLEN+20];
3100         object_type *o_ptr;
3101         cptr q, s;
3102
3103         /* Get an item */
3104 #ifdef JP
3105         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò¸¡º÷¤·¤Þ¤¹¤«? ";
3106         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3107 #else
3108         q = "Enter which item? ";
3109         s = "You have nothing to enter.";
3110 #endif
3111         o_ptr = choose_object(q, s);
3112         if (!o_ptr) return 0;
3113
3114         *o_handle = o_ptr;
3115
3116         string_free(*search_strp);
3117         object_desc(buf, *o_handle, FALSE, 3);
3118         *search_strp = string_make(format("<%s>", buf));
3119         return 1;
3120 }
3121
3122
3123 /*
3124  * Prepare for search by destroyed object
3125  */
3126 static byte get_destroyed_object_for_search(object_type **o_handle, cptr *search_strp)
3127 {
3128         char buf[MAX_NLEN+20];
3129
3130         if (!autopick_last_destroyed_object.k_idx) return 0;
3131
3132         *o_handle = &autopick_last_destroyed_object;
3133
3134         string_free(*search_strp);
3135         object_desc(buf, *o_handle, FALSE, 3);
3136         *search_strp = string_make(format("<%s>", buf));
3137         return 1;
3138 }
3139
3140
3141 /*
3142  * Choose an item or string for search
3143  */
3144 static byte get_string_for_search(object_type **o_handle, cptr *search_strp)
3145 {
3146         int pos = 0;
3147
3148         /*
3149          * Text color
3150          * TERM_YELLOW : Overwrite mode
3151          * TERM_WHITE : Insert mode
3152          */
3153         byte color = TERM_YELLOW;
3154         char buf[MAX_NLEN+20];
3155         const int len = 80;
3156
3157 #ifdef JP
3158         char prompt[] = "¸¡º÷(^I:»ý¤Áʪ ^L:Ç˲õ¤µ¤ì¤¿Êª): ";
3159 #else
3160         char prompt[] = "Search key(^I:inven ^L:destroyed): ";
3161 #endif
3162         int col = sizeof(prompt) - 1;
3163
3164         if (*search_strp) strcpy(buf, *search_strp);
3165         else buf[0] = '\0';
3166
3167         /* Display prompt */
3168         prt(prompt, 0, 0);
3169
3170
3171         /* Process input */
3172         while (TRUE)
3173         {
3174                 bool back = FALSE;
3175                 int skey;
3176
3177                 /* Display the string */
3178                 Term_erase(col, 0, 255);
3179                 Term_putstr(col, 0, -1, color, buf);
3180
3181                 /* Place cursor */
3182                 Term_gotoxy(col + pos, 0);
3183
3184                 /* Get a special key code */
3185                 skey = inkey_special();
3186
3187                 /* Analyze the key */
3188                 switch (skey)
3189                 {
3190                 case SKEY_LEFT:
3191                 case KTRL('b'):
3192                 {
3193                         int i = 0;
3194
3195                         /* Now on insert mode */
3196                         color = TERM_WHITE;
3197
3198                         /* No move at beginning of line */
3199                         if (0 == pos) break;
3200
3201                         while (TRUE)
3202                         {
3203                                 int next_pos = i + 1;
3204
3205 #ifdef JP
3206                                 if (iskanji(buf[i])) next_pos++;
3207 #endif
3208
3209                                 /* Is there the cursor at next position? */ 
3210                                 if (next_pos >= pos) break;
3211
3212                                 /* Move to next */
3213                                 i = next_pos;
3214                         }
3215
3216                         /* Get previous position */
3217                         pos = i;
3218
3219                         break;
3220                 }
3221
3222                 case SKEY_RIGHT:
3223                 case KTRL('f'):
3224                         /* Now on insert mode */
3225                         color = TERM_WHITE;
3226
3227                         /* No move at end of line */
3228                         if ('\0' == buf[pos]) break;
3229
3230 #ifdef JP
3231                         /* Move right */
3232                         if (iskanji(buf[pos])) pos += 2;
3233                         else pos++;
3234 #else
3235                         pos++;
3236 #endif
3237
3238                         break;
3239
3240                 case ESCAPE:
3241                         return 0;
3242
3243                 case KTRL('r'):
3244                         back = TRUE;
3245                         /* Fall through */
3246
3247                 case '\n':
3248                 case '\r':
3249                 case KTRL('s'):
3250                         if (!pos && *o_handle) return (back ? -1 : 1);
3251                         string_free(*search_strp);
3252                         *search_strp = string_make(buf);
3253                         *o_handle = NULL;
3254                         return (back ? -1 : 1);
3255
3256                 case KTRL('i'):
3257                         /* Get an item */
3258                         return get_object_for_search(o_handle, search_strp);
3259
3260                 case KTRL('l'):
3261                         /* Prepare string for destroyed object if there is one. */
3262                         if (get_destroyed_object_for_search(o_handle, search_strp))
3263                                 return 1;
3264                         break;
3265
3266                 case '\010':
3267                 {
3268                         /* Backspace */
3269
3270                         int i = 0;
3271
3272                         /* Now on insert mode */
3273                         color = TERM_WHITE;
3274
3275                         /* No move at beginning of line */
3276                         if (0 == pos) break;
3277
3278                         while (TRUE)
3279                         {
3280                                 int next_pos = i + 1;
3281
3282 #ifdef JP
3283                                 if (iskanji(buf[i])) next_pos++;
3284 #endif
3285
3286                                 /* Is there the cursor at next position? */ 
3287                                 if (next_pos >= pos) break;
3288
3289                                 /* Move to next */
3290                                 i = next_pos;
3291                         }
3292
3293                         /* Get previous position */
3294                         pos = i;
3295
3296                         /* Fall through to 'Delete key' */
3297                 }
3298
3299                 case 0x7F:
3300                 case KTRL('d'):
3301                         /* Delete key */
3302                 {
3303                         int dst, src;
3304
3305                         /* Now on insert mode */
3306                         color = TERM_WHITE;
3307
3308                         /* No move at end of line */
3309                         if ('\0' == buf[pos]) break;
3310
3311                         /* Position of next character */
3312                         src = pos + 1;
3313
3314 #ifdef JP
3315                         /* Next character is one more byte away */
3316                         if (iskanji(buf[pos])) src++;
3317 #endif
3318
3319                         dst = pos;
3320
3321                         /* Move characters at src to dst */
3322                         while ('\0' != (buf[dst++] = buf[src++]))
3323                                 /* loop */;
3324
3325                         break;
3326                 }
3327
3328                 default:
3329                 {
3330                         /* Insert a character */
3331
3332                         char tmp[100];
3333                         char c;
3334
3335                         /* Ignore special keys */
3336                         if (skey & SKEY_MASK) break;
3337
3338                         /* Get a character code */
3339                         c = (char)skey;
3340
3341                         if (color == TERM_YELLOW)
3342                         {
3343                                 /* Overwrite default string */
3344                                 buf[0] = '\0';
3345
3346                                 /* Go to insert mode */
3347                                 color = TERM_WHITE;
3348                         }
3349
3350                         /* Save right part of string */
3351                         strcpy(tmp, buf + pos);
3352 #ifdef JP
3353                         if (iskanji(c))
3354                         {
3355                                 char next;
3356
3357                                 /* Bypass macro processing */
3358                                 inkey_base = TRUE;
3359                                 next = inkey();
3360
3361                                 if (pos + 1 < len)
3362                                 {
3363                                         buf[pos++] = c;
3364                                         buf[pos++] = next;
3365                                 }
3366                                 else
3367                                 {
3368                                         bell();
3369                                 }
3370                         }
3371                         else
3372 #endif
3373                         {
3374 #ifdef JP
3375                                 if (pos < len && (isprint(c) || iskana(c)))
3376 #else
3377                                 if (pos < len && isprint(c))
3378 #endif
3379                                 {
3380                                         buf[pos++] = c;
3381                                 }
3382                                 else
3383                                 {
3384                                         bell();
3385                                 }
3386                         }
3387
3388                         /* Terminate */
3389                         buf[pos] = '\0';
3390
3391                         /* Write back the left part of string */
3392                         my_strcat(buf, tmp, len + 1);
3393
3394                         break;
3395                 } /* default: */
3396
3397                 }
3398
3399         } /* while (TRUE) */
3400 }
3401
3402
3403 /*
3404  * Search next line matches for o_ptr
3405  */
3406 static void search_for_object(text_body_type *tb, object_type *o_ptr, bool forward)
3407 {
3408         autopick_type an_entry, *entry = &an_entry;
3409         char o_name[MAX_NLEN];
3410         int bypassed_cy = -1;
3411
3412         /* Start searching from current cursor position */
3413         int i = tb->cy;
3414
3415         /* Prepare object name string first */
3416         object_desc(o_name, o_ptr, FALSE, 3);
3417
3418         /* Convert the string to lower case */
3419         str_tolower(o_name);
3420
3421         while (TRUE)
3422         {
3423                 /* End of list? */
3424                 if (forward)
3425                 {
3426                         if (!tb->lines_list[++i]) break;
3427                 }
3428                 else
3429                 {
3430                         if (--i < 0) break;
3431                 }
3432
3433                 /* Is this line is a correct entry? */
3434                 if (!autopick_new_entry(entry, tb->lines_list[i], FALSE)) continue;
3435
3436                 /* Does this line match to the object? */
3437                 if (!is_autopick_aux(o_ptr, entry, o_name)) continue;
3438
3439
3440                 /* Found a line but it's inactive */
3441                 if (tb->states[i] & LSTAT_BYPASS)
3442                 {
3443                         /* If it is first found, remember it */
3444                         if (bypassed_cy == -1) bypassed_cy = i;
3445                 }
3446
3447                 /* Found an active line! */
3448                 else
3449                 {
3450                         /* Move to this line */
3451                         tb->cx = 0;
3452                         tb->cy = i;
3453
3454                         if (bypassed_cy != -1)
3455                         {
3456                                 /* Mark as some lines are skipped */
3457                                 tb->dirty_flags |= DIRTY_SKIP_INACTIVE;
3458                         }
3459
3460                         /* Found it! */
3461                         return;
3462                 }
3463         }
3464
3465         if (bypassed_cy != -1)
3466         {
3467                 /* Move to the remembered line */
3468                 tb->cx = 0;
3469                 tb->cy = bypassed_cy;
3470
3471                 /* Mark as this line is inactive */
3472                 tb->dirty_flags |= DIRTY_INACTIVE;
3473         }
3474
3475         else
3476         {
3477                 /* Mark as NOT FOUND */
3478                 tb->dirty_flags |= DIRTY_NOT_FOUND;
3479         }
3480
3481         return;
3482 }
3483
3484
3485 /*
3486  * Search next line matches to the string
3487  */
3488 static void search_for_string(text_body_type *tb, cptr search_str, bool forward)
3489 {
3490         int bypassed_cy = -1;
3491         int bypassed_cx = 0;
3492
3493         /* Start searching from current cursor position */
3494         int i = tb->cy;
3495
3496         while (TRUE)
3497         {
3498                 cptr pos;
3499
3500                 /* End of list? */
3501                 if (forward)
3502                 {
3503                         if (!tb->lines_list[++i]) break;
3504                 }
3505                 else
3506                 {
3507                         if (--i < 0) break;
3508                 }
3509
3510                 /* Look for the string pattern */
3511 #ifdef JP
3512                 pos = strstr_j(tb->lines_list[i], search_str);
3513 #else
3514                 pos = strstr(tb->lines_list[i], search_str);
3515 #endif
3516
3517                 /* Not found! */
3518                 if (!pos) continue;
3519
3520                 /* Found a line but it's inactive */
3521                 if ((tb->states[i] & LSTAT_BYPASS) &&
3522                     !(tb->states[i] & LSTAT_EXPRESSION))
3523                 {
3524                         /* If it is first found, remember it */
3525                         if (bypassed_cy == -1)
3526                         {
3527                                 bypassed_cy = i;
3528                                 bypassed_cx = (int)(pos - tb->lines_list[i]);
3529                         }
3530                 }
3531
3532                 /* Found an active line! */
3533                 else
3534                 {
3535                         /* Move to this location */
3536                         tb->cx = (int)(pos - tb->lines_list[i]);
3537                         tb->cy = i;
3538
3539                         if (bypassed_cy != -1)
3540                         {
3541                                 /* Mark as some lines are skipped */
3542                                 tb->dirty_flags |= DIRTY_SKIP_INACTIVE;
3543                         }
3544
3545                         /* Found it! */
3546                         return;
3547                 }
3548         }
3549
3550         if (bypassed_cy != -1)
3551         {
3552                 /* Move to the remembered line */
3553                 tb->cx = bypassed_cx;
3554                 tb->cy = bypassed_cy;
3555
3556                 /* Mark as this line is inactive */
3557                 tb->dirty_flags |= DIRTY_INACTIVE;
3558         }
3559
3560         else
3561         {
3562                 /* Mark as NOT FOUND */
3563                 tb->dirty_flags |= DIRTY_NOT_FOUND;
3564         }
3565
3566         return;
3567 }
3568
3569
3570
3571
3572 /*
3573  * Editor command id's
3574  */
3575 #define EC_QUIT                1 
3576 #define EC_SAVEQUIT            2     
3577 #define EC_REVERT              3 
3578 #define EC_HELP                4 
3579 #define EC_RETURN              5        
3580 #define EC_LEFT                6 
3581 #define EC_DOWN                7 
3582 #define EC_UP                  8 
3583 #define EC_RIGHT               9 
3584 #define EC_BOL                 10
3585 #define EC_EOL                 11
3586 #define EC_PGUP                12
3587 #define EC_PGDOWN              13
3588 #define EC_TOP                 14
3589 #define EC_BOTTOM              15
3590 #define EC_CUT                 16
3591 #define EC_COPY                17
3592 #define EC_PASTE               18
3593 #define EC_BLOCK               19
3594 #define EC_KILL_LINE           20
3595 #define EC_DELETE_CHAR         21
3596 #define EC_BACKSPACE           22
3597 #define EC_SEARCH_STR          23
3598 #define EC_SEARCH_FORW         24
3599 #define EC_SEARCH_BACK         25
3600 #define EC_SEARCH_OBJ          26
3601 #define EC_SEARCH_DESTROYED    27
3602 #define EC_INSERT_OBJECT       28
3603 #define EC_INSERT_DESTROYED    29
3604 #define EC_INSERT_BLOCK        30
3605 #define EC_INSERT_MACRO        31
3606 #define EC_INSERT_KEYMAP       32
3607 #define EC_CL_AUTOPICK         33
3608 #define EC_CL_DESTROY          34
3609 #define EC_CL_LEAVE            35
3610 #define EC_CL_QUERY            36
3611 #define EC_CL_NO_DISP          37
3612 #define EC_OK_COLLECTING       38
3613 #define EC_IK_UNAWARE          39
3614 #define EC_IK_UNIDENTIFIED     40
3615 #define EC_IK_IDENTIFIED       41
3616 #define EC_IK_STAR_IDENTIFIED  42
3617 #define EC_OK_BOOSTED          43
3618 #define EC_OK_MORE_THAN        44
3619 #define EC_OK_MORE_BONUS       45
3620 #define EC_OK_WORTHLESS        46
3621 #define EC_OK_ARTIFACT         47
3622 #define EC_OK_EGO              48
3623 #define EC_OK_NAMELESS         49
3624 #define EC_OK_WANTED           50
3625 #define EC_OK_UNIQUE           51
3626 #define EC_OK_HUMAN            52
3627 #define EC_OK_UNREADABLE       53
3628 #define EC_OK_REALM1           54
3629 #define EC_OK_REALM2           55
3630 #define EC_OK_FIRST            56
3631 #define EC_OK_SECOND           57
3632 #define EC_OK_THIRD            58
3633 #define EC_OK_FOURTH           59
3634 #define EC_KK_WEAPONS          60
3635 #define EC_KK_FAVORITE         61
3636 #define EC_KK_ARMORS           62
3637 #define EC_KK_MISSILES         63
3638 #define EC_KK_DEVICES          64
3639 #define EC_KK_LIGHTS           65
3640 #define EC_KK_JUNKS            66
3641 #define EC_KK_SPELLBOOKS       67
3642 #define EC_KK_SHIELDS          68
3643 #define EC_KK_BOWS             69
3644 #define EC_KK_RINGS            70
3645 #define EC_KK_AMULETS          71
3646 #define EC_KK_SUITS            72
3647 #define EC_KK_CLOAKS           73
3648 #define EC_KK_HELMS            74
3649 #define EC_KK_GLOVES           75
3650 #define EC_KK_BOOTS            76
3651
3652
3653 /* Manu names */
3654 #ifdef JP
3655
3656 #define MN_QUIT "¥»¡¼¥Ö̵¤·¤Ç½ªÎ»" 
3657 #define MN_SAVEQUIT "¥»¡¼¥Ö¤·¤Æ½ªÎ»" 
3658 #define MN_REVERT "Á´¤Æ¤ÎÊѹ¹¤òÇË´þ" 
3659 #define MN_HELP "¥Ø¥ë¥×" 
3660
3661 #define MN_MOVE "¥«¡¼¥½¥ë°ÜÆ°" 
3662 #define MN_LEFT "º¸" 
3663 #define MN_DOWN "²¼" 
3664 #define MN_UP "¾å" 
3665 #define MN_RIGHT "±¦" 
3666 #define MN_BOL "¹Ô¤ÎÀèƬ" 
3667 #define MN_EOL "¹Ô¤Î½ªÃ¼" 
3668 #define MN_PGUP "¾å¤Î¥Ú¡¼¥¸" 
3669 #define MN_PGDOWN "²¼¤Î¥Ú¡¼¥¸" 
3670 #define MN_TOP "1¹ÔÌܤذÜÆ°" 
3671 #define MN_BOTTOM "ºÇ²¼¹Ô¤Ø°ÜÆ°" 
3672
3673 #define MN_EDIT "ÊÔ½¸" 
3674 #define MN_CUT "¥«¥Ã¥È" 
3675 #define MN_COPY "¥³¥Ô¡¼" 
3676 #define MN_PASTE "¥Ú¡¼¥¹¥È" 
3677 #define MN_BLOCK "ÁªÂòÈϰϤλØÄê" 
3678 #define MN_KILL_LINE "¹Ô¤Î»Ä¤ê¤òºï½ü" 
3679 #define MN_DELETE_CHAR "1ʸ»úºï½ü" 
3680 #define MN_BACKSPACE "¥Ð¥Ã¥¯¥¹¥Ú¡¼¥¹" 
3681 #define MN_RETURN "²þ¹Ô" 
3682 #define MN_RETURN "²þ¹Ô" 
3683
3684 #define MN_SEARCH "¸¡º÷" 
3685 #define MN_SEARCH_STR "ʸ»úÎó¤Ç¸¡º÷" 
3686 #define MN_SEARCH_FORW "Á°Êý¤ØºÆ¸¡º÷" 
3687 #define MN_SEARCH_BACK "¸åÊý¤ØºÆ¸¡º÷" 
3688 #define MN_SEARCH_OBJ "¥¢¥¤¥Æ¥à¤òÁªÂò¤·¤Æ¸¡º÷" 
3689 #define MN_SEARCH_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Ç¸¡º÷" 
3690
3691 #define MN_INSERT "¿§¡¹ÁÞÆþ" 
3692 #define MN_INSERT_OBJECT "ÁªÂò¤·¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3693 #define MN_INSERT_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3694 #define MN_INSERT_BLOCK "¾ò·ïʬ´ô¥Ö¥í¥Ã¥¯¤ÎÎã¤òÁÞÆþ" 
3695 #define MN_INSERT_MACRO "¥Þ¥¯¥íÄêµÁ¤òÁÞÆþ" 
3696 #define MN_INSERT_KEYMAP "¥­¡¼¥Þ¥Ã¥×ÄêµÁ¤òÁÞÆþ" 
3697
3698 #define MN_COMMAND_LETTER "½¦¤¤/Ç˲õ/ÊüÃÖ¤ÎÁªÂò" 
3699 #define MN_CL_AUTOPICK "¡Ö ¡× (¼«Æ°½¦¤¤)" 
3700 #define MN_CL_DESTROY "¡Ö!¡× (¼«Æ°Ç˲õ)" 
3701 #define MN_CL_LEAVE "¡Ö~¡× (ÊüÃÖ)" 
3702 #define MN_CL_QUERY "¡Ö;¡× (³Îǧ¤·¤Æ½¦¤¦)" 
3703 #define MN_CL_NO_DISP "¡Ö(¡× (¥Þ¥Ã¥×¥³¥Þ¥ó¥É¤Çɽ¼¨¤·¤Ê¤¤)" 
3704
3705 #define MN_ADJECTIVE_GEN "·ÁÍÆ»ì(°ìÈÌ)¤ÎÁªÂò" 
3706
3707 #define MN_ADJECTIVE_SPECIAL "·ÁÍÆ»ì(Æüì)¤ÎÁªÂò" 
3708 #define MN_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦 (Éð´ï)" 
3709 #define MN_MORE_THAN "¥À¥¤¥¹ÌÜ # °Ê¾å¤Î (Éð´ï)" 
3710 #define MN_MORE_BONUS "½¤ÀµÃÍ # °Ê¾å¤Î (»ØÎØÅù)" 
3711 #define MN_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È (ÁõÈ÷)" 
3712 #define MN_EGO "¥¨¥´ (ÁõÈ÷)" 
3713 #define MN_NAMELESS "̵ÌäΠ(ÁõÈ÷)" 
3714 #define MN_WANTED "¾Þ¶â¼ó¤Î (»àÂÎ)" 
3715 #define MN_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î (»àÂÎ)" 
3716 #define MN_HUMAN "¿Í´Ö¤Î (»àÂÎ)" 
3717 #define MN_UNREADABLE "Æɤá¤Ê¤¤ (ËâË¡½ñ)" 
3718 #define MN_REALM1 "Âè°ìÎΰè¤Î (ËâË¡½ñ)" 
3719 #define MN_REALM2 "ÂèÆóÎΰè¤Î (ËâË¡½ñ)" 
3720 #define MN_FIRST "1ºýÌܤΠ(ËâË¡½ñ)" 
3721 #define MN_SECOND "2ºýÌܤΠ(ËâË¡½ñ)" 
3722 #define MN_THIRD "3ºýÌܤΠ(ËâË¡½ñ)" 
3723 #define MN_FOURTH "4ºýÌܤΠ(ËâË¡½ñ)" 
3724
3725 #define MN_NOUN "̾»ì¤ÎÁªÂò" 
3726
3727 #else
3728
3729 #define MN_QUIT "Quit without save" 
3730 #define MN_SAVEQUIT "Save & Quit" 
3731 #define MN_REVERT "Revert all changes" 
3732 #define MN_HELP "Help" 
3733
3734 #define MN_MOVE "Move cursor" 
3735 #define MN_LEFT "Left" 
3736 #define MN_DOWN "Down" 
3737 #define MN_UP "Up" 
3738 #define MN_RIGHT "Right" 
3739 #define MN_BOL "Beggining of line" 
3740 #define MN_EOL "End of line" 
3741 #define MN_PGUP "Page up" 
3742 #define MN_PGDOWN "Page down" 
3743 #define MN_TOP "Top" 
3744 #define MN_BOTTOM "Bottom" 
3745
3746 #define MN_EDIT "Edit" 
3747 #define MN_CUT "Cut" 
3748 #define MN_COPY "Copy" 
3749 #define MN_PASTE "Paste" 
3750 #define MN_BLOCK "Select block" 
3751 #define MN_KILL_LINE "Kill rest of line" 
3752 #define MN_DELETE_CHAR "Delete character" 
3753 #define MN_BACKSPACE "Backspace" 
3754 #define MN_RETURN "Return" 
3755 #define MN_RETURN "Return" 
3756
3757 #define MN_SEARCH "Search" 
3758 #define MN_SEARCH_STR "Search by string" 
3759 #define MN_SEARCH_FORW "Search forward" 
3760 #define MN_SEARCH_BACK "Search backward" 
3761 #define MN_SEARCH_OBJ "Search by inventory object" 
3762 #define MN_SEARCH_DESTROYED "Search by destroyed object" 
3763
3764 #define MN_INSERT "Insert..." 
3765 #define MN_INSERT_OBJECT "Insert name of choosen object" 
3766 #define MN_INSERT_DESTROYED "Insert name of destroyed object" 
3767 #define MN_INSERT_BLOCK "Insert conditional block" 
3768 #define MN_INSERT_MACRO "Insert a macro definition" 
3769 #define MN_INSERT_KEYMAP "Insert a keymap definition" 
3770
3771 #define MN_COMMAND_LETTER "Command letter" 
3772 #define MN_CL_AUTOPICK "' ' (Auto pick)" 
3773 #define MN_CL_DESTROY "'!' (Auto destroy)" 
3774 #define MN_CL_LEAVE "'~' (Leave it on the floor)" 
3775 #define MN_CL_QUERY "';' (Query to pick up)" 
3776 #define MN_CL_NO_DISP "'(' (No display on the large map)" 
3777
3778 #define MN_ADJECTIVE_GEN "Adjective (general)" 
3779
3780 #define MN_ADJECTIVE_SPECIAL "Adjective (special)" 
3781 #define MN_BOOSTED "dice boosted (weapons)" 
3782 #define MN_MORE_THAN "more than # dice (weapons)" 
3783 #define MN_MORE_BONUS "more bonus than # (rings etc.)" 
3784 #define MN_ARTIFACT "artifact (equipments)" 
3785 #define MN_EGO "ego (equipments)" 
3786 #define MN_NAMELESS "nameless (equipments)" 
3787 #define MN_WANTED "wanted (corpse)" 
3788 #define MN_UNIQUE "unique (corpse)" 
3789 #define MN_HUMAN "human (corpse)" 
3790 #define MN_UNREADABLE "unreadable (spellbooks)" 
3791 #define MN_REALM1 "realm1 (spellbooks)" 
3792 #define MN_REALM2 "realm2 (spellbooks)" 
3793 #define MN_FIRST "first (spellbooks)" 
3794 #define MN_SECOND "second (spellbooks)" 
3795 #define MN_THIRD "third (spellbooks)" 
3796 #define MN_FOURTH "fourth (spellbooks)" 
3797
3798 #define MN_NOUN "Keywords (noun)" 
3799
3800 #endif
3801
3802
3803 typedef struct {
3804         cptr name;
3805         int level;
3806         int key;
3807         int com_id;
3808 } command_menu_type;
3809
3810
3811 command_menu_type menu_data[] =
3812 {
3813         {MN_HELP, 0, -1, EC_HELP},
3814         {MN_QUIT, 0, KTRL('q'), EC_QUIT}, 
3815         {MN_SAVEQUIT, 0, KTRL('w'), EC_SAVEQUIT}, 
3816         {MN_REVERT, 0, KTRL('z'), EC_REVERT},
3817
3818         {MN_EDIT, 0, -1, -1},
3819         {MN_CUT, 1, KTRL('x'), EC_CUT},
3820         {MN_COPY, 1, KTRL('c'), EC_COPY},
3821         {MN_PASTE, 1, KTRL('v'), EC_PASTE},
3822         {MN_BLOCK, 1, KTRL('g'), EC_BLOCK},
3823         {MN_KILL_LINE, 1, KTRL('k'), EC_KILL_LINE},
3824         {MN_DELETE_CHAR, 1, KTRL('d'), EC_DELETE_CHAR},
3825         {MN_BACKSPACE, 1, KTRL('h'), EC_BACKSPACE},
3826         {MN_RETURN, 1, KTRL('j'), EC_RETURN},
3827         {MN_RETURN, 1, KTRL('m'), EC_RETURN},
3828
3829         {MN_SEARCH, 0, -1, -1},
3830         {MN_SEARCH_STR, 1, KTRL('s'), EC_SEARCH_STR},
3831         {MN_SEARCH_FORW, 1, -1, EC_SEARCH_FORW},
3832         {MN_SEARCH_BACK, 1, KTRL('r'), EC_SEARCH_BACK},
3833         {MN_SEARCH_OBJ, 1, KTRL('y'), EC_SEARCH_OBJ},
3834         {MN_SEARCH_DESTROYED, 1, -1, EC_SEARCH_DESTROYED},
3835
3836         {MN_MOVE, 0, -1, -1},
3837         {MN_LEFT, 1, KTRL('b'), EC_LEFT},
3838         {MN_DOWN, 1, KTRL('n'), EC_DOWN},
3839         {MN_UP, 1, KTRL('p'), EC_UP},
3840         {MN_RIGHT, 1, KTRL('f'), EC_RIGHT},
3841         {MN_BOL, 1, KTRL('a'), EC_BOL},
3842         {MN_EOL, 1, KTRL('e'), EC_EOL},
3843         {MN_PGUP, 1, KTRL('o'), EC_PGUP},
3844         {MN_PGDOWN, 1, KTRL('l'), EC_PGDOWN},
3845         {MN_TOP, 1, KTRL('t'), EC_TOP},
3846         {MN_BOTTOM, 1, KTRL('u'), EC_BOTTOM},
3847
3848         {MN_INSERT, 0, -1, -1},
3849         {MN_INSERT_OBJECT, 1, KTRL('i'), EC_INSERT_OBJECT},
3850         {MN_INSERT_DESTROYED, 1, -1, EC_INSERT_DESTROYED},
3851         {MN_INSERT_BLOCK, 1, -1, EC_INSERT_BLOCK},
3852         {MN_INSERT_MACRO, 1, -1, EC_INSERT_MACRO},
3853         {MN_INSERT_KEYMAP, 1, -1, EC_INSERT_KEYMAP},
3854
3855         {MN_ADJECTIVE_GEN, 0, -1, -1},
3856         {KEY_UNAWARE, 1, -1, EC_IK_UNAWARE},
3857         {KEY_UNIDENTIFIED, 1, -1, EC_IK_UNIDENTIFIED},
3858         {KEY_IDENTIFIED, 1, -1, EC_IK_IDENTIFIED},
3859         {KEY_STAR_IDENTIFIED, 1, -1, EC_IK_STAR_IDENTIFIED},
3860         {KEY_COLLECTING, 1, -1, EC_OK_COLLECTING},
3861         {KEY_WORTHLESS, 1, -1, EC_OK_WORTHLESS},
3862
3863         {MN_ADJECTIVE_SPECIAL, 0, -1, -1},
3864         {MN_BOOSTED, 1, -1, EC_OK_BOOSTED},
3865         {MN_MORE_THAN, 1, -1, EC_OK_MORE_THAN},
3866         {MN_MORE_BONUS, 1, -1, EC_OK_MORE_BONUS},
3867         {MN_ARTIFACT, 1, -1, EC_OK_ARTIFACT},
3868         {MN_EGO, 1, -1, EC_OK_EGO},
3869         {MN_NAMELESS, 1, -1, EC_OK_NAMELESS},
3870         {MN_WANTED, 1, -1, EC_OK_WANTED},
3871         {MN_UNIQUE, 1, -1, EC_OK_UNIQUE},
3872         {MN_HUMAN, 1, -1, EC_OK_HUMAN},
3873         {MN_UNREADABLE, 1, -1, EC_OK_UNREADABLE},
3874         {MN_REALM1, 1, -1, EC_OK_REALM1},
3875         {MN_REALM2, 1, -1, EC_OK_REALM2},
3876         {MN_FIRST, 1, -1, EC_OK_FIRST},
3877         {MN_SECOND, 1, -1, EC_OK_SECOND},
3878         {MN_THIRD, 1, -1, EC_OK_THIRD},
3879         {MN_FOURTH, 1, -1, EC_OK_FOURTH},
3880
3881         {MN_NOUN, 0, -1, -1},
3882         {KEY_WEAPONS, 1, -1, EC_KK_WEAPONS},
3883         {KEY_FAVORITE, 1, -1, EC_KK_FAVORITE},
3884         {KEY_ARMORS, 1, -1, EC_KK_ARMORS},
3885         {KEY_MISSILES, 1, -1, EC_KK_MISSILES},
3886         {KEY_DEVICES, 1, -1, EC_KK_DEVICES},
3887         {KEY_LIGHTS, 1, -1, EC_KK_LIGHTS},
3888         {KEY_JUNKS, 1, -1, EC_KK_JUNKS},
3889         {KEY_SPELLBOOKS, 1, -1, EC_KK_SPELLBOOKS},
3890         {KEY_SHIELDS, 1, -1, EC_KK_SHIELDS},
3891         {KEY_BOWS, 1, -1, EC_KK_BOWS},
3892         {KEY_RINGS, 1, -1, EC_KK_RINGS},
3893         {KEY_AMULETS, 1, -1, EC_KK_AMULETS},
3894         {KEY_SUITS, 1, -1, EC_KK_SUITS},
3895         {KEY_CLOAKS, 1, -1, EC_KK_CLOAKS},
3896         {KEY_HELMS, 1, -1, EC_KK_HELMS},
3897         {KEY_GLOVES, 1, -1, EC_KK_GLOVES},
3898         {KEY_BOOTS, 1, -1, EC_KK_BOOTS},
3899
3900         {MN_COMMAND_LETTER, 0, -1, -1},
3901         {MN_CL_AUTOPICK, 1, -1, EC_CL_AUTOPICK},
3902         {MN_CL_DESTROY, 1, -1, EC_CL_DESTROY},
3903         {MN_CL_LEAVE, 1, -1, EC_CL_LEAVE},
3904         {MN_CL_QUERY, 1, -1, EC_CL_QUERY},
3905         {MN_CL_NO_DISP, 1, -1, EC_CL_NO_DISP},
3906
3907         {MN_DELETE_CHAR, -1, 0x7F, EC_DELETE_CHAR},
3908
3909         {NULL, -1, -1, 0}
3910 };
3911
3912
3913 /*
3914  * Find a command by 'key'.
3915  */
3916 static int get_com_id(char key)
3917 {
3918         int i;
3919
3920         for (i = 0; menu_data[i].name; i++)
3921         {
3922                 if (menu_data[i].key == key)
3923                 {
3924                         return menu_data[i].com_id;
3925                 }
3926         }
3927
3928         return 0;
3929 }
3930
3931
3932 /*
3933  * Display the menu, and get a command 
3934  */
3935 static int do_command_menu(int level, int start)
3936 {
3937         int i;
3938         int max_len = 0;
3939         int max_menu_wid;
3940         int col0 = 5 + level*7;
3941         int row0 = 1 + level*3;
3942         byte menu_key = 0;
3943         int menu_id_list[26];
3944         bool redraw = TRUE;
3945         char linestr[MAX_LINELEN];
3946
3947         /* Get max length */
3948         menu_key = 0;
3949         for (i = start; menu_data[i].level >= level; i++)
3950         {
3951                 int len;
3952
3953                 /* Ignore lower level sub menus */
3954                 if (menu_data[i].level > level) continue;
3955
3956                 len = strlen(menu_data[i].name);
3957                 if (len > max_len) max_len = len;
3958
3959                 menu_id_list[menu_key] = i;
3960                 menu_key++;
3961         }
3962
3963         while (menu_key < 26)
3964         {
3965                 menu_id_list[menu_key] = -1;
3966                 menu_key++;
3967         }
3968
3969         /* Extra space for displaying menu key and command key */
3970         max_menu_wid = max_len + 3 + 3;
3971
3972         /* Prepare box line */
3973         linestr[0] = '\0';
3974         strcat(linestr, "+");
3975         for (i = 0; i < max_menu_wid + 2; i++)
3976         {
3977                 strcat(linestr, "-");
3978         }
3979         strcat(linestr, "+");
3980
3981         while (TRUE)
3982         {
3983                 int com_id;
3984                 char key;
3985                 int menu_id;
3986
3987                 if (redraw)
3988                 {
3989                         int row1 = row0 + 1;
3990
3991                         /* Draw top line */
3992                         Term_putstr(col0, row0, -1, TERM_WHITE, linestr);
3993
3994                         /* Draw menu items */
3995                         menu_key = 0;
3996                         for (i = start; menu_data[i].level >= level; i++)
3997                         {
3998                                 char com_key_str[3];
3999                                 cptr str;
4000
4001                                 /* Ignore lower level sub menus */
4002                                 if (menu_data[i].level > level) continue;
4003
4004                                 if (menu_data[i].com_id == -1)
4005                                 {
4006 #ifdef JP
4007                                         strcpy(com_key_str, "¢§");
4008 #else
4009                                         strcpy(com_key_str, ">");
4010 #endif
4011                                 }
4012                                 else if (menu_data[i].key != -1)
4013                                 {
4014                                         com_key_str[0] = '^';
4015                                         com_key_str[1] = menu_data[i].key + '@';
4016                                         com_key_str[2] = '\0';
4017                                 }
4018                                 else
4019                                 {
4020                                         com_key_str[0] = '\0';
4021                                 }
4022
4023                                 str = format("| %c) %-*s %2s | ", menu_key + 'a', max_len, menu_data[i].name, com_key_str);
4024
4025                                 Term_putstr(col0, row1++, -1, TERM_WHITE, str);
4026
4027                                 menu_key++;
4028                         }
4029
4030                         /* Draw bottom line */
4031                         Term_putstr(col0, row1, -1, TERM_WHITE, linestr);
4032
4033                         /* The menu was shown */
4034                         redraw = FALSE;
4035                 }
4036 #ifdef JP
4037                 prt(format("(a-%c) ¥³¥Þ¥ó¥É:", menu_key + 'a' - 1), 0, 0);
4038 #else
4039                 prt(format("(a-%c) Command:", menu_key + 'a' - 1), 0, 0);
4040 #endif
4041                 key = inkey();
4042
4043                 if (key == ESCAPE) return 0;
4044
4045                 if ('a' <= key && key <= 'z')
4046                 {
4047                         menu_id = menu_id_list[key - 'a'];
4048
4049                         if (menu_id >= 0)
4050                         {
4051                                 com_id = menu_data[menu_id].com_id;
4052
4053                                 if (com_id == -1)
4054                                 {
4055                                         com_id = do_command_menu(level + 1, menu_id + 1);
4056
4057                                         if (com_id) return com_id;
4058                                         else redraw = TRUE;
4059                                 }
4060                                 else if (com_id)
4061                                 {
4062                                         return com_id;
4063                                 }
4064                         }
4065                 }
4066
4067                 else
4068                 {
4069                         com_id = get_com_id(key);
4070                         if (com_id) return com_id;
4071                         else continue;
4072                 }
4073         }
4074 }
4075
4076
4077 static chain_str_type *new_chain_str(cptr str)
4078 {
4079         chain_str_type *chain;
4080
4081         size_t len = strlen(str);
4082
4083         chain = (chain_str_type *)ralloc(sizeof(chain_str_type) + len * sizeof(char));
4084
4085         strcpy(chain->s, str);
4086         chain->next = NULL;
4087
4088         return chain;
4089 }
4090
4091
4092 static void kill_yank_chain(text_body_type *tb)
4093 {
4094         chain_str_type *chain = tb->yank;
4095         tb->yank = NULL;
4096         tb->yank_eol = TRUE;
4097
4098         while (chain)
4099         {
4100                 chain_str_type *next = chain->next;
4101                 size_t len = strlen(chain->s);
4102
4103                 rnfree(chain, sizeof(chain_str_type) + len * sizeof(char));
4104
4105                 chain = next;
4106         }
4107 }
4108
4109
4110 static void add_str_to_yank(text_body_type *tb, cptr str)
4111 {
4112         chain_str_type *chain;
4113
4114         tb->yank_eol = FALSE;
4115
4116         if (NULL == tb->yank)
4117         {
4118                 tb->yank = new_chain_str(str);
4119                 return;
4120         }
4121
4122         chain = tb->yank;
4123
4124         while (TRUE)
4125         {
4126                 if (!chain->next)
4127                 {
4128                         chain->next = new_chain_str(str);
4129                         return;
4130                 }
4131
4132                 /* Go to next */
4133                 chain = chain->next;
4134         }
4135 }
4136
4137
4138 #define DESCRIPT_HGT 3
4139
4140 /*
4141  * Draw text
4142  */
4143 static void draw_text_editor(text_body_type *tb)
4144 {
4145         int i;
4146         int by1 = -1, bx1 = -1, by2 = -1, bx2 = -1;
4147
4148         /* Get size */
4149         Term_get_size(&tb->wid, &tb->hgt);
4150
4151         /*
4152          * Top line (-1), description line (-3), separator (-1)
4153          *  == -5
4154          */
4155         tb->hgt -= 2 + DESCRIPT_HGT;
4156
4157 #ifdef JP
4158         /* Don't let cursor at second byte of kanji */
4159         for (i = 0; tb->lines_list[tb->cy][i]; i++)
4160                 if (iskanji(tb->lines_list[tb->cy][i]))
4161                 {
4162                         i++;
4163                         if (i == tb->cx)
4164                         {
4165                                 tb->cx--;
4166                                 break;
4167                         }
4168                 }
4169 #endif
4170
4171         /* Scroll if necessary */
4172         if (tb->cy < tb->upper || tb->upper + tb->hgt <= tb->cy)
4173                 tb->upper = tb->cy - (tb->hgt)/2;
4174         if (tb->upper < 0)
4175                 tb->upper = 0;
4176         if ((tb->cx < tb->left + 10 && tb->left > 0) || tb->left + tb->wid - 5 <= tb->cx)
4177                 tb->left = tb->cx - (tb->wid)*2/3;
4178         if (tb->left < 0)
4179                 tb->left = 0;
4180
4181         /* Redraw whole window after resize */
4182         if (tb->old_wid != tb->wid || tb->old_hgt != tb->hgt)
4183                 tb->dirty_flags |= DIRTY_SCREEN;
4184
4185         /* Redraw all text after scroll */
4186         else if (tb->old_upper != tb->upper || tb->old_left != tb->left)
4187                 tb->dirty_flags |= DIRTY_ALL;
4188
4189
4190         if (tb->dirty_flags & DIRTY_SCREEN)
4191         {
4192                 tb->dirty_flags |= (DIRTY_ALL | DIRTY_MODE);
4193
4194                 /* Clear screen */
4195                 Term_clear();
4196         }
4197
4198         /* Redraw mode line */
4199         if (tb->dirty_flags & DIRTY_MODE)
4200         {
4201                 char buf[MAX_LINELEN];
4202
4203                 int sepa_length = tb->wid;
4204
4205                 /* Separator */
4206                 for (i = 0; i < sepa_length; i++)
4207                         buf[i] = '-';
4208                 buf[i] = '\0';
4209
4210                 Term_putstr(0, tb->hgt + 1, sepa_length, TERM_WHITE, buf);
4211         }
4212
4213         if (tb->dirty_flags & DIRTY_EXPRESSION)
4214         {
4215                 int y;
4216                 byte state = 0;
4217
4218                 for (y = 0; tb->lines_list[y]; y++)
4219                 {
4220                         char f;
4221                         cptr v;
4222                         cptr s = tb->lines_list[y];
4223
4224                         /* Update this line's state */
4225                         tb->states[y] = state;
4226
4227                         if (*s++ != '?') continue;
4228                         if (*s++ != ':') continue;
4229
4230                         /* Lines below this line are auto-registered */
4231                         if (streq(s, "$AUTOREGISTER"))
4232                                 state |= LSTAT_AUTOREGISTER;
4233
4234                         /* Parse the expr */
4235                         v = process_pref_file_expr(&s, &f);
4236
4237                         /* Set flag */
4238                         if (streq(v, "0")) state |= LSTAT_BYPASS;
4239                         else state &= ~LSTAT_BYPASS;
4240
4241                         /* Re-update this line's state */
4242                         tb->states[y] = state | LSTAT_EXPRESSION;
4243                 }
4244
4245                 tb->dirty_flags |= DIRTY_ALL;
4246         }
4247
4248         if (tb->mark)
4249         {
4250                 int tmp_cx = tb->cx;
4251                 int len = strlen(tb->lines_list[tb->cy]);
4252
4253                 /* Correct cursor location */
4254                 if (tb->cx > len) tmp_cx = len;
4255
4256                 tb->dirty_flags |= DIRTY_ALL;
4257
4258                 if (tb->my < tb->cy ||
4259                     (tb->my == tb->cy && tb->mx < tmp_cx))
4260                 {
4261                         by1 = tb->my;
4262                         bx1 = tb->mx;
4263                         by2 = tb->cy;
4264                         bx2 = tmp_cx;
4265                 }
4266                 else
4267                 {
4268                         by2 = tb->my;
4269                         bx2 = tb->mx;
4270                         by1 = tb->cy;
4271                         bx1 = tmp_cx;
4272                 }
4273         }
4274
4275         /* Dump up to tb->hgt lines of messages */
4276         for (i = 0; i < tb->hgt; i++)
4277         {
4278                 int j;
4279                 int leftcol = 0;
4280                 cptr msg;
4281                 byte color;
4282                 int y = tb->upper+i;
4283
4284                 /* clean or dirty? */
4285                 if (!(tb->dirty_flags & DIRTY_ALL) && (tb->dirty_line != y))
4286                         continue;
4287
4288                 msg = tb->lines_list[y];
4289                 if (!msg) break;
4290
4291                 /* Apply horizontal scroll */
4292                 for (j = 0; *msg; msg++, j++)
4293                 {
4294                         if (j == tb->left) break;
4295 #ifdef JP
4296                         if (j > tb->left)
4297                         {
4298                                 leftcol = 1;
4299                                 break;
4300                         }
4301                         if (iskanji(*msg))
4302                         {
4303                                 msg++;
4304                                 j++;
4305                         }
4306 #endif
4307                 }
4308
4309                 /* Erase line */
4310                 Term_erase(0, i + 1, tb->wid);
4311
4312                 if (tb->states[y] & LSTAT_AUTOREGISTER)
4313                 {
4314                         /* Warning color -- These lines will be deleted later */
4315                         color = TERM_L_RED;
4316                 }
4317                 else
4318                 {
4319                         /* Bypassed line will be displayed by darker color */
4320                         if (tb->states[y] & LSTAT_BYPASS) color = TERM_SLATE;
4321                         else color = TERM_WHITE;
4322                 }
4323
4324                 if (!tb->mark)
4325                 {
4326                         /* Dump the messages, bottom to top */
4327                         Term_putstr(leftcol, i + 1, tb->wid - 1, color, msg);
4328                 }
4329
4330                 else
4331                 {
4332                         int x0 = leftcol + tb->left;
4333
4334                         int sx0 = 0;
4335                         int sx1 = 0;
4336
4337                         if (by1 <= y && y < by2) sx1 = strlen(msg);
4338                         if (y == by1) sx0 = bx1;
4339                         if (y == by2) sx1 = bx2;
4340
4341                         Term_gotoxy(leftcol, i + 1);
4342                         if (x0 < sx0) Term_addstr(sx0 - x0, color, msg);
4343                         if (x0 < sx1) Term_addstr(sx1 - sx0, TERM_YELLOW, msg + (sx0 - x0));
4344                         Term_addstr(-1, color, msg + (sx1 - x0));
4345                 }
4346         }
4347
4348         for (; i < tb->hgt; i++)
4349         {
4350                 /* Erase line */
4351                 Term_erase(0, i + 1, tb->wid);
4352         }
4353
4354         /* Display information when updated */
4355         if (tb->old_cy != tb->cy || (tb->dirty_flags & (DIRTY_ALL | DIRTY_NOT_FOUND | DIRTY_NO_SEARCH)) || tb->dirty_line == tb->cy)
4356         {
4357                 autopick_type an_entry, *entry = &an_entry;
4358                 cptr str1 = NULL, str2 = NULL;
4359
4360
4361                 /* Clear information line */
4362                 for (i = 0; i < DESCRIPT_HGT; i++)
4363                 {
4364                         /* Erase line */
4365                         Term_erase(0, tb->hgt + 2 + i, tb->wid);
4366                 }
4367
4368                 /* Display information */
4369                 if (tb->dirty_flags & DIRTY_NOT_FOUND)
4370                 {
4371 #ifdef JP
4372                         str1 = format("¥Ñ¥¿¡¼¥ó¤¬¸«¤Ä¤«¤ê¤Þ¤»¤ó: %s", tb->search_str);
4373 #else
4374                         str1 = format("Pattern not found: %s", tb->search_str);
4375 #endif
4376                 }
4377                 else if (tb->dirty_flags & DIRTY_SKIP_INACTIVE)
4378                 {
4379 #ifdef JP
4380                         str1 = format("̵¸ú¾õÂ֤ιԤò¥¹¥­¥Ã¥×¤·¤Þ¤·¤¿¡£(%s¤ò¸¡º÷Ãæ)", tb->search_str);
4381 #else
4382                         str1 = format("Some inactive lines are skipped. (Searching %s)", tb->search_str);
4383 #endif
4384                 }
4385                 else if (tb->dirty_flags & DIRTY_INACTIVE)
4386                 {
4387 #ifdef JP
4388                         str1 = format("̵¸ú¾õÂ֤ιԤÀ¤±¤¬¸«ÉÕ¤«¤ê¤Þ¤·¤¿¡£(%s¤ò¸¡º÷Ãæ)", tb->search_str);
4389 #else
4390                         str1 = format("Found only an inactive line. (Searching %s)", tb->search_str);
4391 #endif
4392                 }
4393                 else if (tb->dirty_flags & DIRTY_NO_SEARCH)
4394                 {
4395 #ifdef JP
4396                         str1 = "¸¡º÷¤¹¤ë¥Ñ¥¿¡¼¥ó¤¬¤¢¤ê¤Þ¤»¤ó(^S ¤Ç¸¡º÷)¡£";
4397 #else
4398                         str1 = "No pattern to search. (Press ^S to search.)";
4399 #endif
4400                 }
4401                 else if (tb->lines_list[tb->cy][0] == '#')
4402                 {
4403 #ifdef JP
4404                         str1 = "¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£";
4405 #else
4406                         str1 = "This line is a comment.";
4407 #endif
4408                 }
4409                 else if (tb->lines_list[tb->cy][1] == ':')
4410                 {
4411                         switch(tb->lines_list[tb->cy][0])
4412                         {
4413                         case '?':
4414 #ifdef JP
4415                                 str1 = "¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£";
4416 #else
4417                                 str1 = "This line is a Conditional Expression.";
4418 #endif
4419
4420                                 break;
4421                         case 'A':
4422 #ifdef JP
4423                                 str1 = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£";
4424 #else
4425                                 str1 = "This line defines a Macro action.";
4426 #endif
4427                                 break;
4428                         case 'P':
4429 #ifdef JP
4430                                 str1 = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£";
4431 #else
4432                                 str1 = "This line defines a Macro trigger key.";
4433 #endif
4434                                 break;
4435                         case 'C':
4436 #ifdef JP
4437                                 str1 = "¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£";
4438 #else
4439                                 str1 = "This line defines a Keymap.";
4440 #endif
4441                                 break;
4442                         }
4443
4444                         switch(tb->lines_list[tb->cy][0])
4445                         {
4446                         case '?':
4447                                 if (tb->states[tb->cy] & LSTAT_BYPASS)
4448                                 {
4449 #ifdef JP
4450                                         str2 = "¸½ºß¤Î¼°¤ÎÃͤϡֵ¶(=0)¡×¤Ç¤¹¡£";
4451 #else
4452                                         str2 = "The expression is 'False'(=0) currently.";
4453 #endif
4454                                 }
4455                                 else
4456                                 {
4457 #ifdef JP
4458                                         str2 = "¸½ºß¤Î¼°¤ÎÃͤϡֿ¿(=1)¡×¤Ç¤¹¡£";
4459 #else
4460                                         str2 = "The expression is 'True'(=1) currently.";
4461 #endif
4462                                 }
4463                                 break;
4464
4465                         default:
4466                                 if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4467                                 {
4468 #ifdef JP
4469                                         str2 = "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£";
4470 #else
4471                                         str2 = "This line will be delete later.";
4472 #endif
4473                                 }
4474
4475                                 else if (tb->states[tb->cy] & LSTAT_BYPASS)
4476                                 {
4477 #ifdef JP
4478                                         str2 = "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£";
4479 #else
4480                                         str2 = "This line is bypassed currently.";
4481 #endif
4482                                 }
4483                                 break;
4484                         }
4485                 }
4486
4487                 /* Get description of an autopicker preference line */
4488                 else if (autopick_new_entry(entry, tb->lines_list[tb->cy], FALSE))
4489                 {
4490                         char buf[MAX_LINELEN];
4491                         char temp[MAX_LINELEN];
4492                         cptr t;
4493
4494                         describe_autopick(buf, entry);
4495
4496                         if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4497                         {
4498 #ifdef JP
4499                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£");
4500 #else
4501                                 strcat(buf, "  This line will be delete later.");
4502 #endif
4503                         }
4504
4505                         if (tb->states[tb->cy] & LSTAT_BYPASS)
4506                         {
4507 #ifdef JP
4508                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£");
4509 #else
4510                                 strcat(buf, "  This line is bypassed currently.");
4511 #endif
4512                         }
4513
4514                         roff_to_buf(buf, 81, temp, sizeof(temp));
4515                         t = temp;
4516                         for (i = 0; i < 3; i++)
4517                         {
4518                                 if(t[0] == 0)
4519                                         break; 
4520                                 else
4521                                 {
4522                                         prt(t, tb->hgt +1 + 1 + i, 0);
4523                                         t += strlen(t) + 1;
4524                                 }
4525                         }
4526                         autopick_free_entry(entry);
4527                 }
4528
4529                 /* Draw the first line */
4530                 if (str1) prt(str1, tb->hgt +1 + 1, 0);
4531
4532                 /* Draw the second line */
4533                 if (str2) prt(str2, tb->hgt +1 + 2, 0);
4534         }
4535 }
4536
4537
4538 /*
4539  * Kill segment of a line
4540  */
4541 static void kill_line_segment(text_body_type *tb, int y, int x0, int x1, bool whole)
4542 {
4543         char buf[MAX_LINELEN];
4544         cptr s = tb->lines_list[y];
4545         char *d = buf;
4546         int x;
4547
4548         /* No segment? */
4549         if (x0 == x1) return;
4550
4551         /* Kill whole line? */
4552         if (whole && x0 == 0 && s[x1] == '\0' && tb->lines_list[y+1])
4553         {
4554                 int i;
4555
4556                 string_free(tb->lines_list[y]);
4557
4558                 /* Shift lines up */
4559                 for (i = y; tb->lines_list[i+1]; i++)
4560                         tb->lines_list[i] = tb->lines_list[i+1];
4561                 tb->lines_list[i] = NULL;
4562
4563                 /* Expressions need re-evaluation */
4564                 tb->dirty_flags |= DIRTY_EXPRESSION;
4565
4566                 return;
4567         }
4568
4569         /* Before the segment */
4570         for (x = 0; x < x0; x++)
4571                 *(d++) = s[x];
4572
4573         /* After the segment */
4574         for (x = x1; s[x]; x++)
4575                 *(d++) = s[x];
4576
4577         *d = '\0';
4578
4579         /* Replace */
4580         string_free(tb->lines_list[y]);
4581         tb->lines_list[y] = string_make(buf);
4582
4583         /* Expressions may need re-evaluation */
4584         check_expression_line(tb, y);
4585
4586         /* Text is changed */
4587         tb->changed = TRUE;
4588 }
4589
4590
4591 /*
4592  * Get a trigger key and insert ASCII string for the trigger
4593  */
4594 static bool insert_macro_line(text_body_type *tb)
4595 {
4596         char tmp[1024];
4597         char buf[1024];
4598         int i, n = 0;
4599
4600         /* Flush */
4601         flush();
4602
4603         /* Do not process macros */
4604         inkey_base = TRUE;
4605
4606         /* First key */
4607         i = inkey();
4608
4609         /* Read the pattern */
4610         while (i)
4611         {
4612                 /* Save the key */
4613                 buf[n++] = i;
4614
4615                 /* Do not process macros */
4616                 inkey_base = TRUE;
4617
4618                 /* Do not wait for keys */
4619                 inkey_scan = TRUE;
4620
4621                 /* Attempt to read a key */
4622                 i = inkey();
4623         }
4624
4625         /* Terminate */
4626         buf[n] = '\0';
4627
4628         /* Flush */
4629         flush();
4630
4631         /* Convert the trigger */
4632         ascii_to_text(tmp, buf);
4633
4634         /* Null */
4635         if(!tmp[0]) return FALSE;
4636
4637         tb->cx = 0;
4638
4639         /* Insert preference string */
4640         insert_return_code(tb);
4641         string_free(tb->lines_list[tb->cy]);
4642         tb->lines_list[tb->cy] = string_make(format("P:%s", tmp));
4643
4644         /* Acquire action */
4645         i = macro_find_exact(buf);
4646
4647         if (i == -1)
4648         {
4649                 /* Nothing defined */
4650                 tmp[0] = '\0';
4651         }
4652         else
4653         {
4654                 /* Analyze the current action */
4655                 ascii_to_text(tmp, macro__act[i]);
4656         }
4657
4658         /* Insert blank action preference line */
4659         insert_return_code(tb);
4660         string_free(tb->lines_list[tb->cy]);
4661         tb->lines_list[tb->cy] = string_make(format("A:%s", tmp));
4662
4663         return TRUE;
4664 }
4665
4666
4667 /*
4668  * Get a command key and insert ASCII string for the key
4669  */
4670 static bool insert_keymap_line(text_body_type *tb)
4671 {
4672         char tmp[1024];
4673         char buf[2];
4674         int mode;
4675         cptr act;
4676
4677         /* Roguelike */
4678         if (rogue_like_commands)
4679         {
4680                 mode = KEYMAP_MODE_ROGUE;
4681         }
4682
4683         /* Original */
4684         else
4685         {
4686                 mode = KEYMAP_MODE_ORIG;
4687         }
4688
4689         /* Flush */
4690         flush();
4691
4692         /* Get a key */
4693         buf[0] = inkey();
4694         buf[1] = '\0';
4695
4696         /* Flush */
4697         flush();
4698
4699         /* Convert the trigger */
4700         ascii_to_text(tmp, buf);
4701
4702         /* Null */
4703         if(!tmp[0]) return FALSE;
4704
4705         tb->cx = 0;
4706
4707         /* Insert preference string */
4708         insert_return_code(tb);
4709         string_free(tb->lines_list[tb->cy]);
4710         tb->lines_list[tb->cy] = string_make(format("C:%d:%s", mode, tmp));
4711
4712         /* Look up the keymap */
4713         act = keymap_act[mode][(byte)(buf[0])];
4714
4715         /* Insert blank action preference line */
4716         insert_return_code(tb);
4717         string_free(tb->lines_list[tb->cy]);
4718         tb->lines_list[tb->cy] = string_make(format("A:%s", act));
4719
4720         return TRUE;
4721 }
4722
4723
4724 /*
4725  * Execute a single editor command
4726  */
4727 static bool do_editor_command(text_body_type *tb, int com_id)
4728 {
4729         switch(com_id)
4730         {
4731         case EC_QUIT:
4732                 if (tb->changed)
4733                 {
4734 #ifdef JP
4735                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¤«¤é½ªÎ»¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4736 #else
4737                         if (!get_check("Discard all changes and quit. Are you sure? ")) break;
4738 #endif
4739                 }
4740                 return QUIT_WITHOUT_SAVE;
4741
4742         case EC_SAVEQUIT:
4743                 return QUIT_AND_SAVE;
4744
4745         case EC_REVERT:
4746                 /* Revert to original */
4747 #ifdef JP
4748                 if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4749 #else
4750                 if (!get_check("Discard all changes and revert to original file. Are you sure? ")) break;
4751 #endif
4752
4753                 free_text_lines(tb->lines_list);
4754                 tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
4755                 tb->dirty_flags |= DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
4756                 tb->cx = tb->cy = 0;
4757                 tb->mark = 0;
4758
4759                 /* Text is not changed */
4760                 tb->changed = FALSE;
4761                 break;
4762
4763         case EC_HELP:
4764                 /* Peruse the main help file */
4765 #ifdef JP
4766                 (void)show_file(TRUE, "jeditor.txt", NULL, 0, 0);
4767 #else
4768                 (void)show_file(TRUE, "editor.txt", NULL, 0, 0);
4769 #endif
4770                 /* Redraw all */
4771                 tb->dirty_flags |= DIRTY_SCREEN;
4772
4773                 break;
4774
4775         case EC_RETURN:
4776                 /* Split a line or insert end of line */
4777
4778                 /* Ignore selection */
4779                 if (tb->mark)
4780                 {
4781                         tb->mark = 0;
4782
4783                         /* Now dirty */
4784                         tb->dirty_flags |= DIRTY_ALL;
4785                 }
4786
4787                 insert_return_code(tb);
4788                 tb->cy++;
4789                 tb->cx = 0;
4790
4791                 /* Now dirty */
4792                 tb->dirty_flags |= DIRTY_ALL;
4793                 break;
4794
4795         case EC_LEFT:
4796                 /* Back */
4797                 if (0 < tb->cx)
4798                 {
4799                         int len;
4800
4801                         tb->cx--;
4802                         len = strlen(tb->lines_list[tb->cy]);
4803                         if (len < tb->cx) tb->cx = len;
4804                 }
4805                 else if (tb->cy > 0)
4806                 {
4807                         tb->cy--;
4808                         tb->cx = strlen(tb->lines_list[tb->cy]);
4809                 }
4810                 break;
4811
4812         case EC_DOWN:
4813                 /* Next line */
4814                 if (tb->lines_list[tb->cy + 1]) tb->cy++;
4815                 break;
4816
4817         case EC_UP:
4818                 /* Previous line */
4819                 if (tb->cy > 0) tb->cy--;
4820                 break;
4821
4822         case EC_RIGHT:
4823         {
4824                 /* Forward */
4825
4826                 int len;
4827 #ifdef JP
4828                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
4829 #endif
4830                 tb->cx++;
4831                 len = strlen(tb->lines_list[tb->cy]);
4832                 if (len < tb->cx)
4833                 {
4834                         if (tb->lines_list[tb->cy + 1])
4835                         {
4836                                 tb->cy++;
4837                                 tb->cx = 0;
4838                         }
4839                         else
4840                                 tb->cx = len;
4841                 }
4842                 break;
4843         }
4844
4845         case EC_BOL:
4846                 /* Beginning of line */
4847                 tb->cx = 0;
4848                 break;
4849
4850         case EC_EOL:
4851                 /* End of line */
4852                 tb->cx = strlen(tb->lines_list[tb->cy]);
4853                 break;
4854
4855         case EC_PGUP:
4856                 while (0 < tb->cy && tb->upper <= tb->cy)
4857                         tb->cy--;
4858                 while (0 < tb->upper && tb->cy + 1 < tb->upper + tb->hgt)
4859                         tb->upper--;
4860                 break;
4861
4862         case EC_PGDOWN:
4863                 /* Page down */
4864                 while (tb->cy < tb->upper + tb->hgt && tb->lines_list[tb->cy + 1])
4865                         tb->cy++;
4866                 tb->upper = tb->cy;
4867                 break;
4868
4869         case EC_TOP:
4870                 tb->cy = 0;
4871                 break;
4872
4873         case EC_BOTTOM:
4874                 while (tb->lines_list[tb->cy + 1])
4875                         tb->cy++;
4876                 break;
4877
4878         case EC_CUT:
4879         {       
4880                 int by1, bx1, by2, bx2;
4881                 int y;
4882
4883                 /* Copy the text first */
4884                 do_editor_command(tb, EC_COPY);
4885
4886                 if (tb->my < tb->cy ||
4887                     (tb->my == tb->cy && tb->mx < tb->cx))
4888                 {
4889                         by1 = tb->my;
4890                         bx1 = tb->mx;
4891                         by2 = tb->cy;
4892                         bx2 = tb->cx;
4893                 }
4894                 else
4895                 {
4896                         by2 = tb->my;
4897                         bx2 = tb->mx;
4898                         by1 = tb->cy;
4899                         bx1 = tb->cx;
4900                 }
4901
4902                 /* Kill lines in reverse order */
4903                 for (y = by2; y >= by1; y--)
4904                 {
4905                         int x0 = 0;
4906                         int x1 = strlen(tb->lines_list[y]);
4907
4908                         if (y == by1) x0 = bx1;
4909                         if (y == by2) x1 = bx2;
4910
4911                         kill_line_segment(tb, y, x0, x1, TRUE);
4912                 }
4913
4914                 /* Correct cursor position */
4915                 tb->cy = by1;
4916                 tb->cx = bx1;
4917
4918                 /* Disable selection */
4919                 tb->mark = 0;
4920
4921                 /* Now dirty */
4922                 tb->dirty_flags |= DIRTY_ALL;
4923
4924                 break;
4925         }
4926
4927         case EC_COPY:
4928         {       
4929                 int by1, bx1, by2, bx2;
4930                 int y;
4931                 int len = strlen(tb->lines_list[tb->cy]);
4932
4933                 /* Correct cursor location */
4934                 if (tb->cx > len) tb->cx = len;
4935
4936                 /* Use single line? */
4937                 if (!tb->mark)
4938                 {
4939                         tb->my = tb->cy;
4940                         tb->mx = 0;
4941                         if (tb->lines_list[tb->cy + 1])
4942                         {
4943                                 /* Select a single line */
4944                                 tb->cx = 0;
4945                                 tb->cy++;
4946                         }
4947                         else
4948                         {
4949                                 /* Select bottom line */
4950                                 tb->cx = len;
4951                         }
4952                 }
4953
4954                 if (tb->my < tb->cy ||
4955                     (tb->my == tb->cy && tb->mx < tb->cx))
4956                 {
4957                         by1 = tb->my;
4958                         bx1 = tb->mx;
4959                         by2 = tb->cy;
4960                         bx2 = tb->cx;
4961                 }
4962                 else
4963                 {
4964                         by2 = tb->my;
4965                         bx2 = tb->mx;
4966                         by1 = tb->cy;
4967                         bx1 = tb->cx;
4968                 }
4969
4970                 /* Kill old yank buffer */
4971                 kill_yank_chain(tb);
4972
4973                 /* Copy string to yank buffer */
4974                 for (y = by1; y <= by2; y++)
4975                 {
4976                         int i;
4977                         char buf[MAX_LINELEN];
4978
4979                         int x0 = 0;
4980                         int x1 = strlen(tb->lines_list[y]);
4981
4982                         if (y == by1) x0 = bx1;
4983                         if (y == by2) x1 = bx2;
4984
4985                         for (i = 0; i < x1 - x0; i++)
4986                         {
4987                                 buf[i] = tb->lines_list[y][x0 + i];
4988                         }
4989                         buf[i] = '\0';
4990
4991                         add_str_to_yank(tb, buf);
4992                 }
4993
4994                 /* Disable selection */
4995                 tb->mark = 0;
4996
4997                 /* Now dirty */
4998                 tb->dirty_flags |= DIRTY_ALL;
4999                 break;
5000         }
5001
5002         case EC_PASTE:
5003         {
5004                 /* Paste killed text */
5005
5006                 chain_str_type *chain = tb->yank;
5007                 int len = strlen(tb->lines_list[tb->cy]);
5008
5009                 /* Nothing to do? */
5010                 if (!chain) break;
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                 /* Paste text */
5025                 while (chain)
5026                 {
5027                         cptr yank_str = chain->s;
5028
5029                         char buf[MAX_LINELEN];
5030                         int i;
5031                         char rest[MAX_LINELEN], *rest_ptr = rest;
5032
5033                         /* Save preceding string */
5034                         for(i = 0; i < tb->cx; i++)
5035                                 buf[i] = tb->lines_list[tb->cy][i];
5036
5037                         strcpy(rest, &(tb->lines_list[tb->cy][i]));
5038
5039                         /* Paste yank buffer */
5040                         while (*yank_str && i < MAX_LINELEN-1)
5041                         {
5042                                 buf[i++] = *yank_str++;
5043                         }
5044
5045                         /* Terminate */
5046                         buf[i] = '\0';
5047
5048                         chain = chain->next;
5049
5050                         if (chain || tb->yank_eol)
5051                         {
5052                                 /* There is an end of line between chain nodes */
5053
5054                                 insert_return_code(tb);
5055
5056                                 /* Replace this line with new one */
5057                                 string_free(tb->lines_list[tb->cy]);
5058                                 tb->lines_list[tb->cy] = string_make(buf);
5059
5060                                 /* Move to next line */
5061                                 tb->cx = 0;
5062                                 tb->cy++;
5063
5064                                 continue;
5065                         }
5066
5067                         /* Final line doesn't have end of line */
5068
5069                         tb->cx = strlen(buf);
5070
5071                         /* Rest of original line */
5072                         while (*rest_ptr && i < MAX_LINELEN-1)
5073                         {
5074                                 buf[i++] = *rest_ptr++;
5075                         }
5076
5077                         /* Terminate */
5078                         buf[i] = '\0';
5079
5080                         /* Replace this line with new one */
5081                         string_free(tb->lines_list[tb->cy]);
5082                         tb->lines_list[tb->cy] = string_make(buf);
5083
5084                         /* Finish */
5085                         break;
5086                 }
5087
5088                 /* Now dirty */
5089                 tb->dirty_flags |= DIRTY_ALL;
5090
5091                 /* Expressions need re-evaluation */
5092                 tb->dirty_flags |= DIRTY_EXPRESSION;
5093
5094                 /* Text is changed */
5095                 tb->changed = TRUE;
5096
5097                 break;
5098         }
5099
5100         case EC_BLOCK:
5101                 if (tb->mark)
5102                 {
5103                         /* Disable the selection */
5104                         tb->mark = 0;
5105
5106                         /* Redraw text */
5107                         tb->dirty_flags |= DIRTY_ALL;
5108                 }
5109                 else
5110                 {
5111                         tb->mark = MARK_MARK;
5112
5113                         /* Repeating this command swaps cursor position */
5114                         if (com_id == tb->old_com_id)
5115                         {
5116                                 int tmp;
5117
5118                                 tmp = tb->cy;
5119                                 tb->cy = tb->my;
5120                                 tb->my = tmp;
5121                                 tmp = tb->cx;
5122                                 tb->cx = tb->mx;
5123                                 tb->mx = tmp;
5124
5125                                 /* Redraw text */
5126                                 tb->dirty_flags |= DIRTY_ALL;
5127                         }
5128                         else
5129                         {
5130                                 int len = strlen(tb->lines_list[tb->cy]);
5131
5132                                 /* Mark the point 1 */
5133                                 tb->my = tb->cy;
5134                                 tb->mx = tb->cx;
5135
5136                                 /* Correct cursor location */
5137                                 if (tb->cx > len) tb->mx = len;
5138                         }
5139                 }
5140                 break;
5141
5142         case EC_KILL_LINE:
5143         {
5144                 /* Kill rest of line */
5145
5146                 int len = strlen(tb->lines_list[tb->cy]);
5147
5148                 /* Correct cursor location */
5149                 if (tb->cx > len) tb->cx = len;
5150
5151                 /* Ignore selection */
5152                 if (tb->mark)
5153                 {
5154                         tb->mark = 0;
5155
5156                         /* Now dirty */
5157                         tb->dirty_flags |= DIRTY_ALL;
5158                 }
5159
5160                 /* Append only if this command is repeated. */
5161                 if (tb->old_com_id != com_id)
5162                 {
5163                         kill_yank_chain(tb);
5164                         tb->yank = NULL;
5165                 }
5166
5167                 /* Really deleted some text */
5168                 if (tb->cx < len)
5169                 {
5170                         /* Add deleted string to yank buffer */
5171                         add_str_to_yank(tb, &(tb->lines_list[tb->cy][tb->cx]));
5172
5173                         kill_line_segment(tb, tb->cy, tb->cx, len, FALSE);
5174
5175                         /* Now dirty */
5176                         tb->dirty_line = tb->cy;
5177
5178                         /* Leave end of line character */
5179                         break;
5180                 }
5181
5182                 /* Cut the end of line character only */
5183                 if (tb->yank_eol) add_str_to_yank(tb, "");
5184
5185                 /* Cut end of line */
5186                 tb->yank_eol = TRUE;
5187
5188                 do_editor_command(tb, EC_DELETE_CHAR);
5189                 break;
5190         }
5191
5192         case EC_DELETE_CHAR:
5193                 /* DELETE == go forward + BACK SPACE */
5194
5195                 /* Ignore selection */
5196                 if (tb->mark)
5197                 {
5198                         tb->mark = 0;
5199
5200                         /* Now dirty */
5201                         tb->dirty_flags |= DIRTY_ALL;
5202                 }
5203
5204 #ifdef JP
5205                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
5206 #endif
5207                 tb->cx++;
5208
5209                 do_editor_command(tb, EC_BACKSPACE);
5210                 break;
5211
5212         case EC_BACKSPACE:
5213         {
5214                 /* BACK SPACE */
5215
5216                 int len, i, j, k;
5217                 char buf[MAX_LINELEN];
5218
5219                 /* Ignore selection */
5220                 if (tb->mark)
5221                 {
5222                         tb->mark = 0;
5223
5224                         /* Now dirty */
5225                         tb->dirty_flags |= DIRTY_ALL;
5226                 }
5227
5228                 len = strlen(tb->lines_list[tb->cy]);
5229                 if (len < tb->cx)
5230                 {
5231                         if (tb->lines_list[tb->cy + 1])
5232                         {
5233                                 tb->cy++;
5234                                 tb->cx = 0;
5235                         }
5236                         else
5237                         {
5238                                 tb->cx = len;
5239                                 break;
5240                         }
5241                 }
5242
5243                 if (tb->cx == 0)
5244                 {
5245                         /* delete a return code and union two lines */
5246                         if (tb->cy == 0) break;
5247                         tb->cx = strlen(tb->lines_list[tb->cy-1]);
5248                         strcpy(buf, tb->lines_list[tb->cy-1]);
5249                         strcat(buf, tb->lines_list[tb->cy]);
5250                         string_free(tb->lines_list[tb->cy-1]);
5251                         string_free(tb->lines_list[tb->cy]);
5252                         tb->lines_list[tb->cy-1] = string_make(buf);
5253
5254                         for (i = tb->cy; tb->lines_list[i+1]; i++)
5255                                 tb->lines_list[i] = tb->lines_list[i+1];
5256
5257                         tb->lines_list[i] = NULL;
5258                         tb->cy--;
5259
5260                         /* Now dirty */
5261                         tb->dirty_flags |= DIRTY_ALL;
5262
5263                         /* Expressions need re-evaluation */
5264                         tb->dirty_flags |= DIRTY_EXPRESSION;
5265
5266                         /* Text is changed */
5267                         tb->changed = TRUE;
5268
5269                         break;
5270                 }
5271
5272                 for (i = j = k = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
5273                 {
5274                         k = j;
5275 #ifdef JP
5276                         if (iskanji(tb->lines_list[tb->cy][i]))
5277                                 buf[j++] = tb->lines_list[tb->cy][i++];
5278 #endif
5279                         buf[j++] = tb->lines_list[tb->cy][i];
5280                 }
5281                 while (j > k)
5282                 {
5283                         tb->cx--;
5284                         j--;
5285                 }
5286                 for (; tb->lines_list[tb->cy][i]; i++)
5287                         buf[j++] = tb->lines_list[tb->cy][i];
5288                 buf[j] = '\0';
5289                 string_free(tb->lines_list[tb->cy]);
5290                 tb->lines_list[tb->cy] = string_make(buf);
5291
5292                 /* Now dirty */
5293                 tb->dirty_line = tb->cy;
5294
5295                 /* Expressions may need re-evaluation */
5296                 check_expression_line(tb, tb->cy);
5297
5298                 /* Text is changed */
5299                 tb->changed = TRUE;
5300
5301                 break;
5302         }
5303
5304         case EC_SEARCH_STR:
5305         {
5306                 byte search_dir;
5307
5308                 /* Become dirty because of item/equip menu */
5309                 tb->dirty_flags |= DIRTY_SCREEN;
5310
5311                 search_dir = get_string_for_search(&tb->search_o_ptr, &tb->search_str);
5312
5313                 if (!search_dir) break;
5314
5315                 if (search_dir == 1) do_editor_command(tb, EC_SEARCH_FORW);
5316                 else do_editor_command(tb, EC_SEARCH_BACK);
5317                 break;
5318         }
5319
5320         case EC_SEARCH_FORW:
5321                 if (tb->search_o_ptr)
5322                 {
5323                         search_for_object(tb, tb->search_o_ptr, TRUE);
5324                 }
5325                 else if (tb->search_str)
5326                 {
5327                         search_for_string(tb, tb->search_str, TRUE);
5328                 }
5329                 else
5330                 {
5331                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5332                 }
5333                 break;
5334
5335         case EC_SEARCH_BACK:
5336                 if (tb->search_o_ptr)
5337                 {
5338                         search_for_object(tb, tb->search_o_ptr, FALSE);
5339                 }
5340                 else if (tb->search_str)
5341                 {
5342                         search_for_string(tb, tb->search_str, FALSE);
5343                 }
5344                 else
5345                 {
5346                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5347                 }
5348                 break;
5349
5350         case EC_SEARCH_OBJ:
5351                 /* Become dirty because of item/equip menu */
5352                 tb->dirty_flags |= DIRTY_SCREEN;
5353
5354                 if (!get_object_for_search(&tb->search_o_ptr, &tb->search_str)) break;
5355
5356                 do_editor_command(tb, EC_SEARCH_FORW);
5357                 break;
5358
5359         case EC_SEARCH_DESTROYED:
5360                 if (!get_destroyed_object_for_search(&tb->search_o_ptr, &tb->search_str))
5361                 {
5362                         /* There is no object to search */
5363                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5364
5365                         break;
5366                 }
5367
5368                 do_editor_command(tb, EC_SEARCH_FORW);
5369                 break;
5370
5371         case EC_INSERT_OBJECT:
5372         {
5373                 /* Insert choosen item name */
5374
5375                 autopick_type an_entry, *entry = &an_entry;
5376
5377                 if (!entry_from_choosed_object(entry))
5378                 {
5379                         /* Now dirty because of item/equip menu */
5380                         tb->dirty_flags |= DIRTY_SCREEN;
5381                         break;
5382                 }
5383
5384                 tb->cx = 0;
5385                 insert_return_code(tb);
5386                 string_free(tb->lines_list[tb->cy]);
5387                 tb->lines_list[tb->cy] = autopick_line_from_entry_kill(entry);
5388
5389                 /* Now dirty because of item/equip menu */
5390                 tb->dirty_flags |= DIRTY_SCREEN;
5391
5392                 break;
5393         }
5394
5395         case EC_INSERT_DESTROYED:
5396                 /* Insert a name of last destroyed item */
5397                 if (tb->last_destroyed)
5398                 {
5399                         tb->cx = 0;
5400                         insert_return_code(tb);
5401                         string_free(tb->lines_list[tb->cy]);
5402                         tb->lines_list[tb->cy] = string_make(tb->last_destroyed);
5403
5404                         /* Now dirty */
5405                         tb->dirty_flags |= DIRTY_ALL;
5406
5407                         /* Text is changed */
5408                         tb->changed = TRUE;
5409                 }
5410                 break;
5411
5412         case EC_INSERT_BLOCK:
5413         {
5414                 /* Insert a conditinal expression line */
5415                 char expression[80];
5416
5417                 /* Conditional Expression for Class and Race */
5418                 sprintf(expression, "?:[AND [EQU $RACE %s] [EQU $CLASS %s] [GEQ $LEVEL %02d]]", 
5419 #ifdef JP
5420                         rp_ptr->E_title, cp_ptr->E_title,
5421 #else
5422                         rp_ptr->title, cp_ptr->title,
5423 #endif
5424                         p_ptr->lev
5425                         );
5426
5427                 tb->cx = 0;
5428                 insert_return_code(tb);
5429                 string_free(tb->lines_list[tb->cy]);
5430                 tb->lines_list[tb->cy] = string_make(expression);
5431                 tb->cy++;
5432                 insert_return_code(tb);
5433                 string_free(tb->lines_list[tb->cy]);
5434                 tb->lines_list[tb->cy] = string_make("?:1");
5435
5436                 /* Now dirty */
5437                 tb->dirty_flags |= DIRTY_ALL;
5438
5439                 /* Text is changed */
5440                 tb->changed = TRUE;
5441
5442                 break;
5443         }
5444
5445         case EC_INSERT_MACRO:
5446                 /* Draw_everythig (delete menu) */
5447                 draw_text_editor(tb);
5448
5449                 /* Erase line */
5450                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5451
5452                 /* Prompt */
5453 #ifdef JP
5454                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<¥È¥ê¥¬¡¼¥­¡¼>: ");
5455 #else
5456                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<Trigger key>: ");
5457 #endif
5458                 if (insert_macro_line(tb))
5459                 {
5460                         /* Prepare to input action */
5461                         tb->cx = 2;
5462
5463                         /* Now dirty */
5464                         tb->dirty_flags |= DIRTY_ALL;
5465
5466                         /* Text is changed */
5467                         tb->changed = TRUE;
5468                 }
5469
5470                 break;
5471
5472         case EC_INSERT_KEYMAP:
5473                 /* Draw_everythig (delete menu) */
5474                 draw_text_editor(tb);
5475
5476                 /* Erase line */
5477                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5478
5479                 /* Prompt */
5480 #ifdef JP
5481                 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)));
5482 #else
5483                 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)));
5484 #endif
5485
5486                 if (insert_keymap_line(tb))
5487                 {
5488                         /* Prepare to input action */
5489                         tb->cx = 2;
5490
5491                         /* Now dirty */
5492                         tb->dirty_flags |= DIRTY_ALL;
5493
5494                         /* Text is changed */
5495                         tb->changed = TRUE;
5496                 }                               
5497                 break;
5498
5499         case EC_CL_AUTOPICK: toggle_command_letter(tb, DO_AUTOPICK); break;
5500         case EC_CL_DESTROY: toggle_command_letter(tb, DO_AUTODESTROY); break;
5501         case EC_CL_LEAVE: toggle_command_letter(tb, DONT_AUTOPICK); break;
5502         case EC_CL_QUERY: toggle_command_letter(tb, DO_QUERY_AUTOPICK); break;
5503         case EC_CL_NO_DISP: toggle_command_letter(tb, DO_DISPLAY); break;
5504
5505         case EC_IK_UNAWARE: toggle_keyword(tb, FLG_UNAWARE); break;
5506         case EC_IK_UNIDENTIFIED: toggle_keyword(tb, FLG_UNIDENTIFIED); break;
5507         case EC_IK_IDENTIFIED: toggle_keyword(tb, FLG_IDENTIFIED); break;
5508         case EC_IK_STAR_IDENTIFIED: toggle_keyword(tb, FLG_STAR_IDENTIFIED); break;
5509         case EC_KK_WEAPONS: toggle_keyword(tb, FLG_WEAPONS); break;
5510         case EC_KK_FAVORITE: toggle_keyword(tb, FLG_FAVORITE); break;
5511         case EC_KK_ARMORS: toggle_keyword(tb, FLG_ARMORS); break;
5512         case EC_KK_MISSILES: toggle_keyword(tb, FLG_MISSILES); break;
5513         case EC_KK_DEVICES: toggle_keyword(tb, FLG_DEVICES); break;
5514         case EC_KK_LIGHTS: toggle_keyword(tb, FLG_LIGHTS); break;
5515         case EC_KK_JUNKS: toggle_keyword(tb, FLG_JUNKS); break;
5516         case EC_KK_SPELLBOOKS: toggle_keyword(tb, FLG_SPELLBOOKS); break;
5517         case EC_KK_SHIELDS: toggle_keyword(tb, FLG_SHIELDS); break;
5518         case EC_KK_BOWS: toggle_keyword(tb, FLG_BOWS); break;
5519         case EC_KK_RINGS: toggle_keyword(tb, FLG_RINGS); break;
5520         case EC_KK_AMULETS: toggle_keyword(tb, FLG_AMULETS); break;
5521         case EC_KK_SUITS: toggle_keyword(tb, FLG_SUITS); break;
5522         case EC_KK_CLOAKS: toggle_keyword(tb, FLG_CLOAKS); break;
5523         case EC_KK_HELMS: toggle_keyword(tb, FLG_HELMS); break;
5524         case EC_KK_GLOVES: toggle_keyword(tb, FLG_GLOVES); break;
5525         case EC_KK_BOOTS: toggle_keyword(tb, FLG_BOOTS); break;
5526         case EC_OK_COLLECTING: toggle_keyword(tb, FLG_COLLECTING); break;
5527         case EC_OK_BOOSTED: toggle_keyword(tb, FLG_BOOSTED); break;
5528         case EC_OK_MORE_THAN: toggle_keyword(tb, FLG_MORE_THAN); break;
5529         case EC_OK_MORE_BONUS: toggle_keyword(tb, FLG_MORE_BONUS); break;
5530         case EC_OK_WORTHLESS: toggle_keyword(tb, FLG_WORTHLESS); break;
5531         case EC_OK_ARTIFACT: toggle_keyword(tb, FLG_ARTIFACT); break;
5532         case EC_OK_EGO: toggle_keyword(tb, FLG_EGO); break;
5533         case EC_OK_NAMELESS: toggle_keyword(tb, FLG_NAMELESS); break;
5534         case EC_OK_WANTED: toggle_keyword(tb, FLG_WANTED); break;
5535         case EC_OK_UNIQUE: toggle_keyword(tb, FLG_UNIQUE); break;
5536         case EC_OK_HUMAN: toggle_keyword(tb, FLG_HUMAN); break;
5537         case EC_OK_UNREADABLE:
5538                 toggle_keyword(tb, FLG_UNREADABLE);
5539                 add_keyword(tb, FLG_SPELLBOOKS);
5540                 break;
5541         case EC_OK_REALM1:
5542                 toggle_keyword(tb, FLG_REALM1);
5543                 add_keyword(tb, FLG_SPELLBOOKS);
5544                 break;
5545         case EC_OK_REALM2:
5546                 toggle_keyword(tb, FLG_REALM2);
5547                 add_keyword(tb, FLG_SPELLBOOKS);
5548                 break;
5549         case EC_OK_FIRST:
5550                 toggle_keyword(tb, FLG_FIRST);
5551                 add_keyword(tb, FLG_SPELLBOOKS);
5552                 break;
5553         case EC_OK_SECOND:
5554                 toggle_keyword(tb, FLG_SECOND);
5555                 add_keyword(tb, FLG_SPELLBOOKS);
5556                 break;
5557         case EC_OK_THIRD:
5558                 toggle_keyword(tb, FLG_THIRD);
5559                 add_keyword(tb, FLG_SPELLBOOKS);
5560                 break;
5561         case EC_OK_FOURTH:
5562                 toggle_keyword(tb, FLG_FOURTH);
5563                 add_keyword(tb, FLG_SPELLBOOKS);
5564                 break;
5565         }
5566
5567         /* Save old command */
5568         tb->old_com_id = com_id;
5569
5570         return FALSE;
5571 }
5572
5573
5574 /*
5575  * Insert single letter at cursor position.
5576  */
5577 static void insert_single_letter(text_body_type *tb, int key)
5578 {
5579         int i, j, len;
5580         char buf[MAX_LINELEN];
5581
5582         /* Save preceding string */
5583         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
5584                 buf[j++] = tb->lines_list[tb->cy][i];
5585
5586         /* Add a character */
5587 #ifdef JP
5588         if (iskanji(key))
5589         {
5590                 int next;
5591
5592                 inkey_base = TRUE;
5593                 next = inkey();
5594                 if (j+2 < MAX_LINELEN)
5595                 {
5596                         buf[j++] = key;
5597                         buf[j++] = next;
5598                         tb->cx += 2;
5599                 }
5600                 else
5601                         bell();
5602         }
5603         else
5604 #endif
5605         {
5606                 if (j+1 < MAX_LINELEN)
5607                         buf[j++] = key;
5608                 tb->cx++;
5609         }
5610
5611         /* Add following */
5612         for (; tb->lines_list[tb->cy][i] && j + 1 < MAX_LINELEN; i++)
5613                 buf[j++] = tb->lines_list[tb->cy][i];
5614         buf[j] = '\0';
5615
5616         /* Replace current line with new line */
5617         string_free(tb->lines_list[tb->cy]);
5618         tb->lines_list[tb->cy] = string_make(buf);
5619
5620         /* Move to correct collumn */
5621         len = strlen(tb->lines_list[tb->cy]);
5622         if (len < tb->cx) tb->cx = len;
5623
5624         /* Now dirty */
5625         tb->dirty_line = tb->cy;
5626
5627         /* Expressions may need re-evaluation */
5628         check_expression_line(tb, tb->cy);
5629
5630         /* Text is changed */
5631         tb->changed = TRUE;
5632 }
5633
5634
5635 /*
5636  * Check special key code and get a movement command id
5637  */
5638 static int analyze_move_key(text_body_type *tb, int skey)
5639 {
5640         int com_id;
5641
5642         /* Not a special key */
5643         if (!(skey & SKEY_MASK)) return 0;
5644
5645         /* Convert from a special key code to an editor command */
5646         switch(skey & ~SKEY_MOD_MASK)
5647         {
5648         case SKEY_DOWN:   com_id = EC_DOWN;   break;
5649         case SKEY_LEFT:   com_id = EC_LEFT;   break;
5650         case SKEY_RIGHT:  com_id = EC_RIGHT;  break;
5651         case SKEY_UP:     com_id = EC_UP;     break;
5652         case SKEY_PGUP:   com_id = EC_PGUP;   break;
5653         case SKEY_PGDOWN: com_id = EC_PGDOWN; break;
5654         case SKEY_TOP:    com_id = EC_TOP;    break;
5655         case SKEY_BOTTOM: com_id = EC_BOTTOM; break;
5656
5657         default:
5658                 /* Not a special movement key */
5659                 return 0;
5660         }
5661
5662         /* Without shift modifier */
5663         if (!(skey & SKEY_MOD_SHIFT))
5664         {
5665                 /*
5666                  * Un-shifted cursor keys cancells
5667                  * selection created by shift+cursor.
5668                  */
5669                 if (tb->mark & MARK_BY_SHIFT)
5670                 {
5671                         tb->mark = 0;
5672
5673                         /* Now dirty */
5674                         tb->dirty_flags |= DIRTY_ALL;
5675                 }
5676         }
5677
5678         /* With shift modifier */
5679         else
5680         {
5681                 /* Start selection by shift + cursor keys */
5682                 if (!tb->mark)
5683                 {
5684                         int len = strlen(tb->lines_list[tb->cy]);
5685
5686                         tb->mark = MARK_MARK | MARK_BY_SHIFT;
5687                         tb->my = tb->cy;
5688                         tb->mx = tb->cx;
5689
5690                         /* Correct cursor location */
5691                         if (tb->cx > len) tb->mx = len;
5692                                                 
5693                         /* Need to redraw text */
5694                         if (com_id == EC_UP || com_id == EC_DOWN)
5695                         {
5696                                 /* Redraw all text */
5697                                 tb->dirty_flags |= DIRTY_ALL;
5698                         }
5699                         else
5700                         {
5701                                 tb->dirty_line = tb->cy;
5702                         }
5703                 }
5704         }
5705
5706         return com_id;
5707 }
5708
5709 /*
5710  * In-game editor of Object Auto-picker/Destoryer
5711  */
5712 void do_cmd_edit_autopick(void)
5713 {
5714         static int cx_save = 0;
5715         static int cy_save = 0;
5716
5717         text_body_type text_body, *tb = &text_body;
5718
5719         autopick_type an_entry, *entry = &an_entry;
5720         char buf[MAX_LINELEN];
5721
5722         int i;
5723         int key = -1;
5724
5725         static s32b old_autosave_turn = 0L;
5726         byte quit = 0;
5727
5728         tb->changed = FALSE;
5729         tb->cx = cx_save;
5730         tb->cy = cy_save;
5731         tb->upper = tb->left = 0;
5732         tb->mark = 0;
5733         tb->mx = tb->my = 0;
5734         tb->old_cy = tb->old_upper = tb->old_left = -1;
5735         tb->old_wid = tb->old_hgt = -1;
5736         tb->old_com_id = 0;
5737
5738         tb->yank = NULL;
5739         tb->search_o_ptr = NULL;
5740         tb->search_str = NULL;
5741         tb->last_destroyed = NULL;
5742         tb->dirty_flags = DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
5743         tb->dirty_line = -1;
5744         tb->filename_mode = PT_DEFAULT;
5745
5746         /* Autosave */
5747         if (turn > old_autosave_turn + 100L)
5748         {
5749                 do_cmd_save_game(TRUE);
5750                 old_autosave_turn = turn;
5751         }
5752
5753         /* HACK -- Reset start_time to stop counting playtime while edit */
5754         update_playtime();
5755
5756         /* Free old entries */
5757         init_autopicker();
5758
5759         /* Command Description of the 'Last Destroyed Item' */
5760         if (autopick_last_destroyed_object.k_idx)
5761         {
5762                 autopick_entry_from_object(entry, &autopick_last_destroyed_object);
5763                 tb->last_destroyed = autopick_line_from_entry_kill(entry);
5764         }
5765
5766         /* Read or initialize whole text */
5767         tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
5768
5769         /* Reset cursor position if needed */
5770         for (i = 0; i < tb->cy; i++)
5771         {
5772                 if (!tb->lines_list[i])
5773                 {
5774                         tb->cy = tb->cx = 0;
5775                         break;
5776                 }
5777         }
5778
5779         /* Save the screen */
5780         screen_save();
5781
5782         /* Process requests until done */
5783         while (!quit)
5784         {
5785                 int com_id = 0;
5786
5787                 /* Draw_everythig */
5788                 draw_text_editor(tb);
5789
5790                 /* Display header line */
5791 #ifdef JP
5792                 prt("(^Q:½ªÎ» ^W:¥»¡¼¥Ö¤·¤Æ½ªÎ», ESC:¥á¥Ë¥å¡¼, ¤½¤Î¾:ÆþÎÏ)", 0, 0);
5793 #else   
5794                 prt("(^Q:quit, ^W:save&quit, ESC:menu, Other:input text)", 0, 0);
5795 #endif
5796                 if (!tb->mark)
5797                 {
5798                         /* Display current position */
5799                         prt (format("(%d,%d)", tb->cx, tb->cy), 0, 60);
5800                 }
5801                 else
5802                 {
5803                         /* Display current position and mark position */
5804                         prt (format("(%d,%d)-(%d,%d)", tb->mx, tb->my, tb->cx, tb->cy), 0, 60);
5805                 }
5806
5807                 /* Place cursor */
5808                 Term_gotoxy(tb->cx - tb->left, tb->cy - tb->upper + 1);
5809
5810                 /* Now clean */
5811                 tb->dirty_flags = 0;
5812                 tb->dirty_line = -1;
5813
5814                 /* Save old key and location */
5815                 tb->old_cy = tb->cy;
5816                 tb->old_upper = tb->upper;
5817                 tb->old_left = tb->left;
5818                 tb->old_wid = tb->wid;
5819                 tb->old_hgt = tb->hgt;
5820
5821                 /* Get a command */
5822                 key = inkey_special();
5823
5824                 /* Special keys */
5825                 if (key & SKEY_MASK)
5826                 {
5827                         /* Get a movement command */
5828                         com_id = analyze_move_key(tb, key);
5829                 }
5830
5831                 /* Open the menu */
5832                 else if (key == ESCAPE)
5833                 {
5834                         com_id = do_command_menu(0, 0);
5835
5836                         /* Redraw all text later */
5837                         tb->dirty_flags |= DIRTY_SCREEN;
5838                 }
5839
5840                 /* Insert a character */
5841                 else if (!iscntrl((unsigned char)key))
5842                 {
5843                         /* Ignore selection */
5844                         if (tb->mark)
5845                         {
5846                                 tb->mark = 0;
5847
5848                                 /* Now dirty */
5849                                 tb->dirty_flags |= DIRTY_ALL;
5850                         }
5851
5852                         insert_single_letter(tb, key);
5853
5854                         /* Next loop */
5855                         continue;
5856                 }
5857
5858                 /* Other commands */
5859                 else
5860                 {
5861                         com_id = get_com_id(key);
5862                 }
5863
5864                 if (com_id) quit = do_editor_command(tb, com_id);
5865         } /* while (TRUE) */
5866
5867         /* Restore the screen */
5868         screen_load();
5869
5870         /* Get the filename of preference */
5871         strcpy(buf, pickpref_filename(tb->filename_mode));
5872
5873         if (quit == QUIT_AND_SAVE)
5874                 write_text_lines(buf, tb->lines_list);
5875
5876         free_text_lines(tb->lines_list);
5877
5878         string_free(tb->last_destroyed);
5879
5880         /* Destroy string chain */
5881         kill_yank_chain(tb);
5882
5883         /* Reload autopick pref */
5884         process_pickpref_file(buf);
5885
5886         /* HACK -- reset start_time so that playtime is not increase while edit */
5887         start_time = time(NULL);
5888
5889         /* Save cursor location */
5890         cx_save = tb->cx;
5891         cy_save = tb->cy;
5892 }